blob: 83415dd42d0dd85fd2f64b60c1cbf85df9c79ef1 [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
74#define EEEPC_WMI_DEVID_WWAN3G 0x00010019
Corentin Chary33e0e6f2011-02-06 13:28:34 +010075#define EEEPC_WMI_DEVID_BACKLIGHT 0x00050012
Corentin Chary9e1565b2011-02-06 13:28:36 +010076#define EEEPC_WMI_DEVID_CAMERA 0x00060013
77#define EEEPC_WMI_DEVID_CARDREADER 0x00080013
Corentin Chary33e0e6f2011-02-06 13:28:34 +010078#define EEEPC_WMI_DEVID_TPDLED 0x00100011
Yong Wang3d7b1652010-04-11 09:27:54 +080079
Corentin Charyaafa7192011-02-06 13:28:35 +010080#define EEEPC_WMI_DSTS_STATUS_BIT 0x00000001
81#define EEEPC_WMI_DSTS_PRESENCE_BIT 0x00010000
82
Corentin Charyafa7c882011-02-06 13:28:28 +010083static bool hotplug_wireless;
84
85module_param(hotplug_wireless, bool, 0444);
86MODULE_PARM_DESC(hotplug_wireless,
87 "Enable hotplug for wireless device. "
88 "If your laptop needs that, please report to "
89 "acpi4asus-user@lists.sourceforge.net.");
90
Yong Wangee027e42010-03-21 10:26:34 +080091static const struct key_entry eeepc_wmi_keymap[] = {
92 /* Sleep already handled via generic ACPI code */
Yong Wangee027e42010-03-21 10:26:34 +080093 { KE_IGNORE, NOTIFY_BRNDOWN_MIN, { KEY_BRIGHTNESSDOWN } },
94 { KE_IGNORE, NOTIFY_BRNUP_MIN, { KEY_BRIGHTNESSUP } },
Corentin Chary5628e5a2011-02-06 13:28:26 +010095 { KE_KEY, 0x30, { KEY_VOLUMEUP } },
96 { KE_KEY, 0x31, { KEY_VOLUMEDOWN } },
97 { KE_KEY, 0x32, { KEY_MUTE } },
98 { KE_KEY, 0x5c, { KEY_F15 } },
99 { KE_KEY, 0x5d, { KEY_WLAN } },
Chris Bagwelleda17482010-10-11 18:47:17 -0500100 { KE_KEY, 0x6b, { KEY_F13 } }, /* Disable Touchpad */
Corentin Charybc40cce2011-02-06 13:28:27 +0100101 { KE_KEY, 0x88, { KEY_WLAN } },
Corentin Chary5628e5a2011-02-06 13:28:26 +0100102 { KE_KEY, 0xcc, { KEY_SWITCHVIDEOMODE } },
103 { KE_KEY, 0xe0, { KEY_PROG1 } },
Chris Bagwelleda17482010-10-11 18:47:17 -0500104 { KE_KEY, 0xe1, { KEY_F14 } },
105 { KE_KEY, 0xe9, { KEY_DISPLAY_OFF } },
Yong Wangee027e42010-03-21 10:26:34 +0800106 { KE_END, 0},
107};
108
Yong Wang3d7b1652010-04-11 09:27:54 +0800109struct bios_args {
110 u32 dev_id;
111 u32 ctrl_param;
112};
113
Corentin Chary8c1b2d82010-11-29 08:14:09 +0100114/*
115 * eeepc-wmi/ - debugfs root directory
116 * dev_id - current dev_id
117 * ctrl_param - current ctrl_param
118 * devs - call DEVS(dev_id, ctrl_param) and print result
119 * dsts - call DSTS(dev_id) and print result
120 */
121struct eeepc_wmi_debug {
122 struct dentry *root;
123 u32 dev_id;
124 u32 ctrl_param;
125};
126
Yong Wang81248882010-04-11 09:26:33 +0800127struct eeepc_wmi {
Corentin Charyafa7c882011-02-06 13:28:28 +0100128 bool hotplug_wireless;
129
Yong Wang81248882010-04-11 09:26:33 +0800130 struct input_dev *inputdev;
Yong Wang3d7b1652010-04-11 09:27:54 +0800131 struct backlight_device *backlight_device;
Corentin Chary27c136c2010-11-29 08:14:05 +0100132 struct platform_device *platform_device;
Corentin Chary084fca62010-11-29 08:14:06 +0100133
134 struct led_classdev tpd_led;
135 int tpd_led_wk;
136 struct workqueue_struct *led_workqueue;
137 struct work_struct tpd_led_work;
Corentin Charyba48fdb2010-11-29 08:14:07 +0100138
139 struct rfkill *wlan_rfkill;
140 struct rfkill *bluetooth_rfkill;
141 struct rfkill *wwan3g_rfkill;
Corentin Chary8c1b2d82010-11-29 08:14:09 +0100142
Corentin Charyafa7c882011-02-06 13:28:28 +0100143 struct hotplug_slot *hotplug_slot;
144 struct mutex hotplug_lock;
Corentin Chary279f8f92011-02-06 13:28:29 +0100145 struct mutex wmi_lock;
146 struct workqueue_struct *hotplug_workqueue;
147 struct work_struct hotplug_work;
Corentin Charyafa7c882011-02-06 13:28:28 +0100148
Corentin Chary8c1b2d82010-11-29 08:14:09 +0100149 struct eeepc_wmi_debug debug;
Yong Wang81248882010-04-11 09:26:33 +0800150};
151
Yong Wang81248882010-04-11 09:26:33 +0800152static int eeepc_wmi_input_init(struct eeepc_wmi *eeepc)
Yong Wangee027e42010-03-21 10:26:34 +0800153{
154 int err;
155
Yong Wang81248882010-04-11 09:26:33 +0800156 eeepc->inputdev = input_allocate_device();
157 if (!eeepc->inputdev)
Yong Wangee027e42010-03-21 10:26:34 +0800158 return -ENOMEM;
159
Yong Wang81248882010-04-11 09:26:33 +0800160 eeepc->inputdev->name = "Eee PC WMI hotkeys";
Yong Wang45f2c692010-04-11 09:27:19 +0800161 eeepc->inputdev->phys = EEEPC_WMI_FILE "/input0";
Yong Wang81248882010-04-11 09:26:33 +0800162 eeepc->inputdev->id.bustype = BUS_HOST;
Corentin Chary27c136c2010-11-29 08:14:05 +0100163 eeepc->inputdev->dev.parent = &eeepc->platform_device->dev;
Yong Wangee027e42010-03-21 10:26:34 +0800164
Yong Wang81248882010-04-11 09:26:33 +0800165 err = sparse_keymap_setup(eeepc->inputdev, eeepc_wmi_keymap, NULL);
Yong Wangee027e42010-03-21 10:26:34 +0800166 if (err)
167 goto err_free_dev;
168
Yong Wang81248882010-04-11 09:26:33 +0800169 err = input_register_device(eeepc->inputdev);
Yong Wangee027e42010-03-21 10:26:34 +0800170 if (err)
171 goto err_free_keymap;
172
173 return 0;
174
175err_free_keymap:
Yong Wang81248882010-04-11 09:26:33 +0800176 sparse_keymap_free(eeepc->inputdev);
Yong Wangee027e42010-03-21 10:26:34 +0800177err_free_dev:
Yong Wang81248882010-04-11 09:26:33 +0800178 input_free_device(eeepc->inputdev);
Yong Wangee027e42010-03-21 10:26:34 +0800179 return err;
180}
181
Yong Wang81248882010-04-11 09:26:33 +0800182static void eeepc_wmi_input_exit(struct eeepc_wmi *eeepc)
183{
184 if (eeepc->inputdev) {
185 sparse_keymap_free(eeepc->inputdev);
186 input_unregister_device(eeepc->inputdev);
187 }
188
189 eeepc->inputdev = NULL;
190}
191
Corentin Chary2a3f0062010-11-29 08:14:10 +0100192static acpi_status eeepc_wmi_get_devstate(u32 dev_id, u32 *retval)
Yong Wang3d7b1652010-04-11 09:27:54 +0800193{
194 struct acpi_buffer input = { (acpi_size)sizeof(u32), &dev_id };
195 struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER, NULL };
196 union acpi_object *obj;
197 acpi_status status;
198 u32 tmp;
199
200 status = wmi_evaluate_method(EEEPC_WMI_MGMT_GUID,
Corentin Charyafa7c882011-02-06 13:28:28 +0100201 1, EEEPC_WMI_METHODID_DSTS,
202 &input, &output);
Yong Wang3d7b1652010-04-11 09:27:54 +0800203
204 if (ACPI_FAILURE(status))
205 return status;
206
207 obj = (union acpi_object *)output.pointer;
208 if (obj && obj->type == ACPI_TYPE_INTEGER)
209 tmp = (u32)obj->integer.value;
210 else
211 tmp = 0;
212
Corentin Chary2a3f0062010-11-29 08:14:10 +0100213 if (retval)
214 *retval = tmp;
Yong Wang3d7b1652010-04-11 09:27:54 +0800215
216 kfree(obj);
217
218 return status;
219
220}
221
Corentin Chary8c1b2d82010-11-29 08:14:09 +0100222static acpi_status eeepc_wmi_set_devstate(u32 dev_id, u32 ctrl_param,
223 u32 *retval)
Yong Wang3d7b1652010-04-11 09:27:54 +0800224{
225 struct bios_args args = {
226 .dev_id = dev_id,
227 .ctrl_param = ctrl_param,
228 };
229 struct acpi_buffer input = { (acpi_size)sizeof(args), &args };
230 acpi_status status;
231
Corentin Chary8c1b2d82010-11-29 08:14:09 +0100232 if (!retval) {
233 status = wmi_evaluate_method(EEEPC_WMI_MGMT_GUID, 1,
234 EEEPC_WMI_METHODID_DEVS,
235 &input, NULL);
236 } else {
237 struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER, NULL };
238 union acpi_object *obj;
239 u32 tmp;
240
241 status = wmi_evaluate_method(EEEPC_WMI_MGMT_GUID, 1,
242 EEEPC_WMI_METHODID_DEVS,
243 &input, &output);
244
245 if (ACPI_FAILURE(status))
246 return status;
247
248 obj = (union acpi_object *)output.pointer;
249 if (obj && obj->type == ACPI_TYPE_INTEGER)
250 tmp = (u32)obj->integer.value;
251 else
252 tmp = 0;
253
254 *retval = tmp;
255
256 kfree(obj);
257 }
Yong Wang3d7b1652010-04-11 09:27:54 +0800258
259 return status;
260}
261
Corentin Chary5c956382011-02-06 13:28:31 +0100262/* Helper for special devices with magic return codes */
263static int eeepc_wmi_get_devstate_simple(u32 dev_id)
264{
265 u32 retval = 0;
266 acpi_status status;
267
268 status = eeepc_wmi_get_devstate(dev_id, &retval);
269
270 if (ACPI_FAILURE(status))
271 return -EINVAL;
272
Corentin Charyaafa7192011-02-06 13:28:35 +0100273 if (!(retval & EEEPC_WMI_DSTS_PRESENCE_BIT))
Corentin Chary5c956382011-02-06 13:28:31 +0100274 return -ENODEV;
275
Corentin Charyaafa7192011-02-06 13:28:35 +0100276 return retval & EEEPC_WMI_DSTS_STATUS_BIT;
Corentin Chary5c956382011-02-06 13:28:31 +0100277}
278
Corentin Chary084fca62010-11-29 08:14:06 +0100279/*
280 * LEDs
281 */
282/*
283 * These functions actually update the LED's, and are called from a
284 * workqueue. By doing this as separate work rather than when the LED
285 * subsystem asks, we avoid messing with the Eeepc ACPI stuff during a
286 * potentially bad time, such as a timer interrupt.
287 */
288static void tpd_led_update(struct work_struct *work)
289{
290 int ctrl_param;
291 struct eeepc_wmi *eeepc;
292
293 eeepc = container_of(work, struct eeepc_wmi, tpd_led_work);
294
295 ctrl_param = eeepc->tpd_led_wk;
Corentin Chary8c1b2d82010-11-29 08:14:09 +0100296 eeepc_wmi_set_devstate(EEEPC_WMI_DEVID_TPDLED, ctrl_param, NULL);
Corentin Chary084fca62010-11-29 08:14:06 +0100297}
298
299static void tpd_led_set(struct led_classdev *led_cdev,
300 enum led_brightness value)
301{
302 struct eeepc_wmi *eeepc;
303
304 eeepc = container_of(led_cdev, struct eeepc_wmi, tpd_led);
305
306 eeepc->tpd_led_wk = !!value;
307 queue_work(eeepc->led_workqueue, &eeepc->tpd_led_work);
308}
309
310static int read_tpd_state(struct eeepc_wmi *eeepc)
311{
Corentin Chary5c956382011-02-06 13:28:31 +0100312 return eeepc_wmi_get_devstate_simple(EEEPC_WMI_DEVID_TPDLED);
Corentin Chary084fca62010-11-29 08:14:06 +0100313}
314
315static enum led_brightness tpd_led_get(struct led_classdev *led_cdev)
316{
317 struct eeepc_wmi *eeepc;
318
319 eeepc = container_of(led_cdev, struct eeepc_wmi, tpd_led);
320
321 return read_tpd_state(eeepc);
322}
323
324static int eeepc_wmi_led_init(struct eeepc_wmi *eeepc)
325{
326 int rv;
327
328 if (read_tpd_state(eeepc) < 0)
329 return 0;
330
331 eeepc->led_workqueue = create_singlethread_workqueue("led_workqueue");
332 if (!eeepc->led_workqueue)
333 return -ENOMEM;
334 INIT_WORK(&eeepc->tpd_led_work, tpd_led_update);
335
336 eeepc->tpd_led.name = "eeepc::touchpad";
337 eeepc->tpd_led.brightness_set = tpd_led_set;
338 eeepc->tpd_led.brightness_get = tpd_led_get;
339 eeepc->tpd_led.max_brightness = 1;
340
341 rv = led_classdev_register(&eeepc->platform_device->dev,
342 &eeepc->tpd_led);
343 if (rv) {
344 destroy_workqueue(eeepc->led_workqueue);
345 return rv;
346 }
347
348 return 0;
349}
350
351static void eeepc_wmi_led_exit(struct eeepc_wmi *eeepc)
352{
353 if (eeepc->tpd_led.dev)
354 led_classdev_unregister(&eeepc->tpd_led);
355 if (eeepc->led_workqueue)
356 destroy_workqueue(eeepc->led_workqueue);
357}
358
359/*
Corentin Charyafa7c882011-02-06 13:28:28 +0100360 * PCI hotplug (for wlan rfkill)
361 */
362static bool eeepc_wlan_rfkill_blocked(struct eeepc_wmi *eeepc)
363{
Corentin Chary5c956382011-02-06 13:28:31 +0100364 int result = eeepc_wmi_get_devstate_simple(EEEPC_WMI_DEVID_WLAN);
Corentin Charyafa7c882011-02-06 13:28:28 +0100365
Corentin Chary5c956382011-02-06 13:28:31 +0100366 if (result < 0)
Corentin Charyafa7c882011-02-06 13:28:28 +0100367 return false;
Corentin Chary5c956382011-02-06 13:28:31 +0100368 return !result;
Corentin Charyafa7c882011-02-06 13:28:28 +0100369}
370
371static void eeepc_rfkill_hotplug(struct eeepc_wmi *eeepc)
372{
373 struct pci_dev *dev;
374 struct pci_bus *bus;
Corentin Chary279f8f92011-02-06 13:28:29 +0100375 bool blocked;
Corentin Charyafa7c882011-02-06 13:28:28 +0100376 bool absent;
377 u32 l;
378
Corentin Chary279f8f92011-02-06 13:28:29 +0100379 mutex_lock(&eeepc->wmi_lock);
380 blocked = eeepc_wlan_rfkill_blocked(eeepc);
381 mutex_unlock(&eeepc->wmi_lock);
Corentin Charyafa7c882011-02-06 13:28:28 +0100382
383 mutex_lock(&eeepc->hotplug_lock);
384
Corentin Chary279f8f92011-02-06 13:28:29 +0100385 if (eeepc->wlan_rfkill)
386 rfkill_set_sw_state(eeepc->wlan_rfkill, blocked);
387
Corentin Charyafa7c882011-02-06 13:28:28 +0100388 if (eeepc->hotplug_slot) {
389 bus = pci_find_bus(0, 1);
390 if (!bus) {
391 pr_warning("Unable to find PCI bus 1?\n");
392 goto out_unlock;
393 }
394
395 if (pci_bus_read_config_dword(bus, 0, PCI_VENDOR_ID, &l)) {
396 pr_err("Unable to read PCI config space?\n");
397 goto out_unlock;
398 }
399 absent = (l == 0xffffffff);
400
401 if (blocked != absent) {
402 pr_warning("BIOS says wireless lan is %s, "
403 "but the pci device is %s\n",
404 blocked ? "blocked" : "unblocked",
405 absent ? "absent" : "present");
406 pr_warning("skipped wireless hotplug as probably "
407 "inappropriate for this model\n");
408 goto out_unlock;
409 }
410
411 if (!blocked) {
412 dev = pci_get_slot(bus, 0);
413 if (dev) {
414 /* Device already present */
415 pci_dev_put(dev);
416 goto out_unlock;
417 }
418 dev = pci_scan_single_device(bus, 0);
419 if (dev) {
420 pci_bus_assign_resources(bus);
421 if (pci_bus_add_device(dev))
422 pr_err("Unable to hotplug wifi\n");
423 }
424 } else {
425 dev = pci_get_slot(bus, 0);
426 if (dev) {
427 pci_remove_bus_device(dev);
428 pci_dev_put(dev);
429 }
430 }
431 }
432
433out_unlock:
434 mutex_unlock(&eeepc->hotplug_lock);
435}
436
437static void eeepc_rfkill_notify(acpi_handle handle, u32 event, void *data)
438{
439 struct eeepc_wmi *eeepc = data;
440
441 if (event != ACPI_NOTIFY_BUS_CHECK)
442 return;
443
Corentin Chary279f8f92011-02-06 13:28:29 +0100444 /*
445 * We can't call directly eeepc_rfkill_hotplug because most
446 * of the time WMBC is still being executed and not reetrant.
447 * There is currently no way to tell ACPICA that we want this
448 * method to be serialized, we schedule a eeepc_rfkill_hotplug
449 * call later, in a safer context.
450 */
451 queue_work(eeepc->hotplug_workqueue, &eeepc->hotplug_work);
Corentin Charyafa7c882011-02-06 13:28:28 +0100452}
453
454static int eeepc_register_rfkill_notifier(struct eeepc_wmi *eeepc,
455 char *node)
456{
457 acpi_status status;
458 acpi_handle handle;
459
460 status = acpi_get_handle(NULL, node, &handle);
461
462 if (ACPI_SUCCESS(status)) {
463 status = acpi_install_notify_handler(handle,
464 ACPI_SYSTEM_NOTIFY,
465 eeepc_rfkill_notify,
466 eeepc);
467 if (ACPI_FAILURE(status))
468 pr_warning("Failed to register notify on %s\n", node);
469 } else
470 return -ENODEV;
471
472 return 0;
473}
474
475static void eeepc_unregister_rfkill_notifier(struct eeepc_wmi *eeepc,
476 char *node)
477{
478 acpi_status status = AE_OK;
479 acpi_handle handle;
480
481 status = acpi_get_handle(NULL, node, &handle);
482
483 if (ACPI_SUCCESS(status)) {
484 status = acpi_remove_notify_handler(handle,
485 ACPI_SYSTEM_NOTIFY,
486 eeepc_rfkill_notify);
487 if (ACPI_FAILURE(status))
488 pr_err("Error removing rfkill notify handler %s\n",
489 node);
490 }
491}
492
493static int eeepc_get_adapter_status(struct hotplug_slot *hotplug_slot,
494 u8 *value)
495{
Corentin Chary5c956382011-02-06 13:28:31 +0100496 int result = eeepc_wmi_get_devstate_simple(EEEPC_WMI_DEVID_WLAN);
Corentin Charyafa7c882011-02-06 13:28:28 +0100497
Corentin Chary5c956382011-02-06 13:28:31 +0100498 if (result < 0)
499 return result;
Corentin Charyafa7c882011-02-06 13:28:28 +0100500
Corentin Chary5c956382011-02-06 13:28:31 +0100501 *value = !!result;
Corentin Charyafa7c882011-02-06 13:28:28 +0100502 return 0;
503}
504
505static void eeepc_cleanup_pci_hotplug(struct hotplug_slot *hotplug_slot)
506{
507 kfree(hotplug_slot->info);
508 kfree(hotplug_slot);
509}
510
511static struct hotplug_slot_ops eeepc_hotplug_slot_ops = {
512 .owner = THIS_MODULE,
513 .get_adapter_status = eeepc_get_adapter_status,
514 .get_power_status = eeepc_get_adapter_status,
515};
516
Corentin Chary279f8f92011-02-06 13:28:29 +0100517static void eeepc_hotplug_work(struct work_struct *work)
518{
519 struct eeepc_wmi *eeepc;
520
521 eeepc = container_of(work, struct eeepc_wmi, hotplug_work);
522 eeepc_rfkill_hotplug(eeepc);
523}
524
Corentin Charyafa7c882011-02-06 13:28:28 +0100525static int eeepc_setup_pci_hotplug(struct eeepc_wmi *eeepc)
526{
527 int ret = -ENOMEM;
528 struct pci_bus *bus = pci_find_bus(0, 1);
529
530 if (!bus) {
531 pr_err("Unable to find wifi PCI bus\n");
532 return -ENODEV;
533 }
534
Corentin Chary279f8f92011-02-06 13:28:29 +0100535 eeepc->hotplug_workqueue =
536 create_singlethread_workqueue("hotplug_workqueue");
537 if (!eeepc->hotplug_workqueue)
538 goto error_workqueue;
539
540 INIT_WORK(&eeepc->hotplug_work, eeepc_hotplug_work);
541
Corentin Charyafa7c882011-02-06 13:28:28 +0100542 eeepc->hotplug_slot = kzalloc(sizeof(struct hotplug_slot), GFP_KERNEL);
543 if (!eeepc->hotplug_slot)
544 goto error_slot;
545
546 eeepc->hotplug_slot->info = kzalloc(sizeof(struct hotplug_slot_info),
547 GFP_KERNEL);
548 if (!eeepc->hotplug_slot->info)
549 goto error_info;
550
551 eeepc->hotplug_slot->private = eeepc;
552 eeepc->hotplug_slot->release = &eeepc_cleanup_pci_hotplug;
553 eeepc->hotplug_slot->ops = &eeepc_hotplug_slot_ops;
554 eeepc_get_adapter_status(eeepc->hotplug_slot,
555 &eeepc->hotplug_slot->info->adapter_status);
556
557 ret = pci_hp_register(eeepc->hotplug_slot, bus, 0, "eeepc-wifi");
558 if (ret) {
559 pr_err("Unable to register hotplug slot - %d\n", ret);
560 goto error_register;
561 }
562
563 return 0;
564
565error_register:
566 kfree(eeepc->hotplug_slot->info);
567error_info:
568 kfree(eeepc->hotplug_slot);
569 eeepc->hotplug_slot = NULL;
570error_slot:
Corentin Chary279f8f92011-02-06 13:28:29 +0100571 destroy_workqueue(eeepc->hotplug_workqueue);
572error_workqueue:
Corentin Charyafa7c882011-02-06 13:28:28 +0100573 return ret;
574}
575
576/*
Corentin Charyba48fdb2010-11-29 08:14:07 +0100577 * Rfkill devices
578 */
579static int eeepc_rfkill_set(void *data, bool blocked)
580{
581 int dev_id = (unsigned long)data;
582 u32 ctrl_param = !blocked;
Corentin Chary7898cf12011-02-06 13:28:30 +0100583 acpi_status status;
Corentin Charyba48fdb2010-11-29 08:14:07 +0100584
Corentin Chary7898cf12011-02-06 13:28:30 +0100585 status = eeepc_wmi_set_devstate(dev_id, ctrl_param, NULL);
586
587 if (ACPI_FAILURE(status))
588 return -EIO;
589
590 return 0;
Corentin Charyba48fdb2010-11-29 08:14:07 +0100591}
592
593static void eeepc_rfkill_query(struct rfkill *rfkill, void *data)
594{
595 int dev_id = (unsigned long)data;
Corentin Chary5c956382011-02-06 13:28:31 +0100596 int result;
Corentin Charyba48fdb2010-11-29 08:14:07 +0100597
Corentin Chary5c956382011-02-06 13:28:31 +0100598 result = eeepc_wmi_get_devstate_simple(dev_id);
Corentin Charyba48fdb2010-11-29 08:14:07 +0100599
Corentin Chary5c956382011-02-06 13:28:31 +0100600 if (result < 0)
Corentin Charyba48fdb2010-11-29 08:14:07 +0100601 return ;
602
Corentin Chary5c956382011-02-06 13:28:31 +0100603 rfkill_set_sw_state(rfkill, !result);
Corentin Charyba48fdb2010-11-29 08:14:07 +0100604}
605
Corentin Chary279f8f92011-02-06 13:28:29 +0100606static int eeepc_rfkill_wlan_set(void *data, bool blocked)
607{
608 struct eeepc_wmi *eeepc = data;
609 int ret;
610
611 /*
612 * This handler is enabled only if hotplug is enabled.
613 * In this case, the eeepc_wmi_set_devstate() will
614 * trigger a wmi notification and we need to wait
615 * this call to finish before being able to call
616 * any wmi method
617 */
618 mutex_lock(&eeepc->wmi_lock);
619 ret = eeepc_rfkill_set((void *)(long)EEEPC_WMI_DEVID_WLAN, blocked);
620 mutex_unlock(&eeepc->wmi_lock);
621 return ret;
622}
623
624static void eeepc_rfkill_wlan_query(struct rfkill *rfkill, void *data)
625{
626 eeepc_rfkill_query(rfkill, (void *)(long)EEEPC_WMI_DEVID_WLAN);
627}
628
629static const struct rfkill_ops eeepc_rfkill_wlan_ops = {
630 .set_block = eeepc_rfkill_wlan_set,
631 .query = eeepc_rfkill_wlan_query,
632};
633
Corentin Charyba48fdb2010-11-29 08:14:07 +0100634static const struct rfkill_ops eeepc_rfkill_ops = {
635 .set_block = eeepc_rfkill_set,
636 .query = eeepc_rfkill_query,
637};
638
639static int eeepc_new_rfkill(struct eeepc_wmi *eeepc,
640 struct rfkill **rfkill,
641 const char *name,
642 enum rfkill_type type, int dev_id)
643{
Corentin Chary5c956382011-02-06 13:28:31 +0100644 int result = eeepc_wmi_get_devstate_simple(dev_id);
Corentin Charyba48fdb2010-11-29 08:14:07 +0100645
Corentin Chary5c956382011-02-06 13:28:31 +0100646 if (result < 0)
647 return result;
Corentin Charyba48fdb2010-11-29 08:14:07 +0100648
Corentin Chary279f8f92011-02-06 13:28:29 +0100649 if (dev_id == EEEPC_WMI_DEVID_WLAN && eeepc->hotplug_wireless)
650 *rfkill = rfkill_alloc(name, &eeepc->platform_device->dev, type,
651 &eeepc_rfkill_wlan_ops, eeepc);
652 else
653 *rfkill = rfkill_alloc(name, &eeepc->platform_device->dev, type,
654 &eeepc_rfkill_ops, (void *)(long)dev_id);
Corentin Charyba48fdb2010-11-29 08:14:07 +0100655
656 if (!*rfkill)
657 return -EINVAL;
658
Corentin Chary5c956382011-02-06 13:28:31 +0100659 rfkill_init_sw_state(*rfkill, !result);
Corentin Charyba48fdb2010-11-29 08:14:07 +0100660 result = rfkill_register(*rfkill);
661 if (result) {
662 rfkill_destroy(*rfkill);
663 *rfkill = NULL;
664 return result;
665 }
666 return 0;
667}
668
669static void eeepc_wmi_rfkill_exit(struct eeepc_wmi *eeepc)
670{
Corentin Charyafa7c882011-02-06 13:28:28 +0100671 eeepc_unregister_rfkill_notifier(eeepc, "\\_SB.PCI0.P0P5");
672 eeepc_unregister_rfkill_notifier(eeepc, "\\_SB.PCI0.P0P6");
673 eeepc_unregister_rfkill_notifier(eeepc, "\\_SB.PCI0.P0P7");
Corentin Charyba48fdb2010-11-29 08:14:07 +0100674 if (eeepc->wlan_rfkill) {
675 rfkill_unregister(eeepc->wlan_rfkill);
676 rfkill_destroy(eeepc->wlan_rfkill);
677 eeepc->wlan_rfkill = NULL;
678 }
Corentin Charyafa7c882011-02-06 13:28:28 +0100679 /*
680 * Refresh pci hotplug in case the rfkill state was changed after
681 * eeepc_unregister_rfkill_notifier()
682 */
683 eeepc_rfkill_hotplug(eeepc);
684 if (eeepc->hotplug_slot)
685 pci_hp_deregister(eeepc->hotplug_slot);
Corentin Chary279f8f92011-02-06 13:28:29 +0100686 if (eeepc->hotplug_workqueue)
687 destroy_workqueue(eeepc->hotplug_workqueue);
Corentin Charyafa7c882011-02-06 13:28:28 +0100688
Corentin Charyba48fdb2010-11-29 08:14:07 +0100689 if (eeepc->bluetooth_rfkill) {
690 rfkill_unregister(eeepc->bluetooth_rfkill);
691 rfkill_destroy(eeepc->bluetooth_rfkill);
692 eeepc->bluetooth_rfkill = NULL;
693 }
694 if (eeepc->wwan3g_rfkill) {
695 rfkill_unregister(eeepc->wwan3g_rfkill);
696 rfkill_destroy(eeepc->wwan3g_rfkill);
697 eeepc->wwan3g_rfkill = NULL;
698 }
699}
700
701static int eeepc_wmi_rfkill_init(struct eeepc_wmi *eeepc)
702{
703 int result = 0;
704
Corentin Charyafa7c882011-02-06 13:28:28 +0100705 mutex_init(&eeepc->hotplug_lock);
Corentin Chary279f8f92011-02-06 13:28:29 +0100706 mutex_init(&eeepc->wmi_lock);
Corentin Charyafa7c882011-02-06 13:28:28 +0100707
Corentin Charyba48fdb2010-11-29 08:14:07 +0100708 result = eeepc_new_rfkill(eeepc, &eeepc->wlan_rfkill,
709 "eeepc-wlan", RFKILL_TYPE_WLAN,
710 EEEPC_WMI_DEVID_WLAN);
711
712 if (result && result != -ENODEV)
713 goto exit;
714
715 result = eeepc_new_rfkill(eeepc, &eeepc->bluetooth_rfkill,
716 "eeepc-bluetooth", RFKILL_TYPE_BLUETOOTH,
717 EEEPC_WMI_DEVID_BLUETOOTH);
718
719 if (result && result != -ENODEV)
720 goto exit;
721
722 result = eeepc_new_rfkill(eeepc, &eeepc->wwan3g_rfkill,
723 "eeepc-wwan3g", RFKILL_TYPE_WWAN,
724 EEEPC_WMI_DEVID_WWAN3G);
725
726 if (result && result != -ENODEV)
727 goto exit;
728
Corentin Charyafa7c882011-02-06 13:28:28 +0100729 result = eeepc_setup_pci_hotplug(eeepc);
730 /*
731 * If we get -EBUSY then something else is handling the PCI hotplug -
732 * don't fail in this case
733 */
734 if (result == -EBUSY)
735 result = 0;
736
737 eeepc_register_rfkill_notifier(eeepc, "\\_SB.PCI0.P0P5");
738 eeepc_register_rfkill_notifier(eeepc, "\\_SB.PCI0.P0P6");
739 eeepc_register_rfkill_notifier(eeepc, "\\_SB.PCI0.P0P7");
740 /*
741 * Refresh pci hotplug in case the rfkill state was changed during
742 * setup.
743 */
744 eeepc_rfkill_hotplug(eeepc);
745
Corentin Charyba48fdb2010-11-29 08:14:07 +0100746exit:
747 if (result && result != -ENODEV)
748 eeepc_wmi_rfkill_exit(eeepc);
749
750 if (result == -ENODEV)
751 result = 0;
752
753 return result;
754}
755
756/*
Corentin Chary084fca62010-11-29 08:14:06 +0100757 * Backlight
758 */
Yong Wang3d7b1652010-04-11 09:27:54 +0800759static int read_brightness(struct backlight_device *bd)
760{
Corentin Charydfed65d2010-11-29 08:14:12 +0100761 u32 retval;
Yong Wang3d7b1652010-04-11 09:27:54 +0800762 acpi_status status;
763
Corentin Chary2a3f0062010-11-29 08:14:10 +0100764 status = eeepc_wmi_get_devstate(EEEPC_WMI_DEVID_BACKLIGHT, &retval);
Yong Wang3d7b1652010-04-11 09:27:54 +0800765
766 if (ACPI_FAILURE(status))
767 return -1;
768 else
Corentin Chary2a3f0062010-11-29 08:14:10 +0100769 return retval & 0xFF;
Yong Wang3d7b1652010-04-11 09:27:54 +0800770}
771
772static int update_bl_status(struct backlight_device *bd)
773{
774
Corentin Charydfed65d2010-11-29 08:14:12 +0100775 u32 ctrl_param;
Yong Wang3d7b1652010-04-11 09:27:54 +0800776 acpi_status status;
777
778 ctrl_param = bd->props.brightness;
779
Corentin Chary8c1b2d82010-11-29 08:14:09 +0100780 status = eeepc_wmi_set_devstate(EEEPC_WMI_DEVID_BACKLIGHT,
781 ctrl_param, NULL);
Yong Wang3d7b1652010-04-11 09:27:54 +0800782
783 if (ACPI_FAILURE(status))
784 return -1;
785 else
786 return 0;
787}
788
789static const struct backlight_ops eeepc_wmi_bl_ops = {
790 .get_brightness = read_brightness,
791 .update_status = update_bl_status,
792};
793
794static int eeepc_wmi_backlight_notify(struct eeepc_wmi *eeepc, int code)
795{
796 struct backlight_device *bd = eeepc->backlight_device;
797 int old = bd->props.brightness;
Daniel Mackb7670ed2010-05-19 12:37:01 +0200798 int new = old;
Yong Wang3d7b1652010-04-11 09:27:54 +0800799
800 if (code >= NOTIFY_BRNUP_MIN && code <= NOTIFY_BRNUP_MAX)
801 new = code - NOTIFY_BRNUP_MIN + 1;
802 else if (code >= NOTIFY_BRNDOWN_MIN && code <= NOTIFY_BRNDOWN_MAX)
803 new = code - NOTIFY_BRNDOWN_MIN;
804
805 bd->props.brightness = new;
806 backlight_update_status(bd);
807 backlight_force_update(bd, BACKLIGHT_UPDATE_HOTKEY);
808
809 return old;
810}
811
812static int eeepc_wmi_backlight_init(struct eeepc_wmi *eeepc)
813{
814 struct backlight_device *bd;
815 struct backlight_properties props;
816
817 memset(&props, 0, sizeof(struct backlight_properties));
818 props.max_brightness = 15;
819 bd = backlight_device_register(EEEPC_WMI_FILE,
Corentin Chary27c136c2010-11-29 08:14:05 +0100820 &eeepc->platform_device->dev, eeepc,
Yong Wang3d7b1652010-04-11 09:27:54 +0800821 &eeepc_wmi_bl_ops, &props);
822 if (IS_ERR(bd)) {
823 pr_err("Could not register backlight device\n");
824 return PTR_ERR(bd);
825 }
826
827 eeepc->backlight_device = bd;
828
829 bd->props.brightness = read_brightness(bd);
830 bd->props.power = FB_BLANK_UNBLANK;
831 backlight_update_status(bd);
832
833 return 0;
834}
835
836static void eeepc_wmi_backlight_exit(struct eeepc_wmi *eeepc)
837{
838 if (eeepc->backlight_device)
839 backlight_device_unregister(eeepc->backlight_device);
840
841 eeepc->backlight_device = NULL;
842}
843
844static void eeepc_wmi_notify(u32 value, void *context)
845{
846 struct eeepc_wmi *eeepc = context;
847 struct acpi_buffer response = { ACPI_ALLOCATE_BUFFER, NULL };
848 union acpi_object *obj;
849 acpi_status status;
850 int code;
851 int orig_code;
852
853 status = wmi_get_event_data(value, &response);
854 if (status != AE_OK) {
855 pr_err("bad event status 0x%x\n", status);
856 return;
857 }
858
859 obj = (union acpi_object *)response.pointer;
860
861 if (obj && obj->type == ACPI_TYPE_INTEGER) {
862 code = obj->integer.value;
863 orig_code = code;
864
865 if (code >= NOTIFY_BRNUP_MIN && code <= NOTIFY_BRNUP_MAX)
866 code = NOTIFY_BRNUP_MIN;
867 else if (code >= NOTIFY_BRNDOWN_MIN &&
868 code <= NOTIFY_BRNDOWN_MAX)
869 code = NOTIFY_BRNDOWN_MIN;
870
871 if (code == NOTIFY_BRNUP_MIN || code == NOTIFY_BRNDOWN_MIN) {
872 if (!acpi_video_backlight_support())
873 eeepc_wmi_backlight_notify(eeepc, orig_code);
874 }
875
876 if (!sparse_keymap_report_event(eeepc->inputdev,
877 code, 1, true))
878 pr_info("Unknown key %x pressed\n", code);
879 }
880
881 kfree(obj);
882}
883
Corentin Chary9e1565b2011-02-06 13:28:36 +0100884/*
885 * Sys helpers
886 */
887static int parse_arg(const char *buf, unsigned long count, int *val)
888{
889 if (!count)
890 return 0;
891 if (sscanf(buf, "%i", val) != 1)
892 return -EINVAL;
893 return count;
894}
895
896static ssize_t store_sys_wmi(int devid, const char *buf, size_t count)
897{
898 acpi_status status;
899 u32 retval;
900 int rv, value;
901
902 value = eeepc_wmi_get_devstate_simple(devid);
903 if (value == -ENODEV) /* Check device presence */
904 return value;
905
906 rv = parse_arg(buf, count, &value);
907 status = eeepc_wmi_set_devstate(devid, value, &retval);
908
909 if (ACPI_FAILURE(status))
910 return -EIO;
911 return rv;
912}
913
914static ssize_t show_sys_wmi(int devid, char *buf)
915{
916 int value = eeepc_wmi_get_devstate_simple(devid);
917
918 if (value < 0)
919 return value;
920
921 return sprintf(buf, "%d\n", value);
922}
923
924#define EEEPC_WMI_CREATE_DEVICE_ATTR(_name, _mode, _cm) \
925 static ssize_t show_##_name(struct device *dev, \
926 struct device_attribute *attr, \
927 char *buf) \
928 { \
929 return show_sys_wmi(_cm, buf); \
930 } \
931 static ssize_t store_##_name(struct device *dev, \
932 struct device_attribute *attr, \
933 const char *buf, size_t count) \
934 { \
935 return store_sys_wmi(_cm, buf, count); \
936 } \
937 static struct device_attribute dev_attr_##_name = { \
938 .attr = { \
939 .name = __stringify(_name), \
940 .mode = _mode }, \
941 .show = show_##_name, \
942 .store = store_##_name, \
943 }
944
945EEEPC_WMI_CREATE_DEVICE_ATTR(camera, 0644, EEEPC_WMI_DEVID_CAMERA);
946EEEPC_WMI_CREATE_DEVICE_ATTR(cardr, 0644, EEEPC_WMI_DEVID_CARDREADER);
947
Dmitry Torokhov67fa38e2010-11-03 11:14:01 -0700948static ssize_t store_cpufv(struct device *dev, struct device_attribute *attr,
949 const char *buf, size_t count)
Chris Bagwell7f80d732010-10-11 18:47:18 -0500950{
951 int value;
952 struct acpi_buffer input = { (acpi_size)sizeof(value), &value };
953 acpi_status status;
954
955 if (!count || sscanf(buf, "%i", &value) != 1)
956 return -EINVAL;
957 if (value < 0 || value > 2)
958 return -EINVAL;
959
960 status = wmi_evaluate_method(EEEPC_WMI_MGMT_GUID,
961 1, EEEPC_WMI_METHODID_CFVS, &input, NULL);
962
963 if (ACPI_FAILURE(status))
964 return -EIO;
965 else
966 return count;
967}
968
969static DEVICE_ATTR(cpufv, S_IRUGO | S_IWUSR, NULL, store_cpufv);
970
Corentin Chary4e37b422010-11-29 08:14:08 +0100971static struct attribute *platform_attributes[] = {
972 &dev_attr_cpufv.attr,
Corentin Chary9e1565b2011-02-06 13:28:36 +0100973 &dev_attr_camera.attr,
974 &dev_attr_cardr.attr,
Corentin Chary4e37b422010-11-29 08:14:08 +0100975 NULL
976};
977
Corentin Chary9e1565b2011-02-06 13:28:36 +0100978static mode_t eeepc_sysfs_is_visible(struct kobject *kobj,
979 struct attribute *attr,
980 int idx)
981{
982 bool supported = true;
983 int devid = -1;
984
985 if (attr == &dev_attr_camera.attr)
986 devid = EEEPC_WMI_DEVID_CAMERA;
987 else if (attr == &dev_attr_cardr.attr)
988 devid = EEEPC_WMI_DEVID_CARDREADER;
989
990 if (devid != -1)
991 supported = eeepc_wmi_get_devstate_simple(devid) != -ENODEV;
992
993 return supported ? attr->mode : 0;
994}
995
Corentin Chary4e37b422010-11-29 08:14:08 +0100996static struct attribute_group platform_attribute_group = {
Corentin Chary9e1565b2011-02-06 13:28:36 +0100997 .is_visible = eeepc_sysfs_is_visible,
998 .attrs = platform_attributes
Corentin Chary4e37b422010-11-29 08:14:08 +0100999};
1000
Chris Bagwell7f80d732010-10-11 18:47:18 -05001001static void eeepc_wmi_sysfs_exit(struct platform_device *device)
1002{
Corentin Chary4e37b422010-11-29 08:14:08 +01001003 sysfs_remove_group(&device->dev.kobj, &platform_attribute_group);
Chris Bagwell7f80d732010-10-11 18:47:18 -05001004}
1005
1006static int eeepc_wmi_sysfs_init(struct platform_device *device)
1007{
Corentin Chary4e37b422010-11-29 08:14:08 +01001008 return sysfs_create_group(&device->dev.kobj, &platform_attribute_group);
Chris Bagwell7f80d732010-10-11 18:47:18 -05001009}
1010
Corentin Chary27c136c2010-11-29 08:14:05 +01001011/*
1012 * Platform device
1013 */
1014static int __init eeepc_wmi_platform_init(struct eeepc_wmi *eeepc)
1015{
Corentin Charya04ce292011-02-06 13:28:33 +01001016 return eeepc_wmi_sysfs_init(eeepc->platform_device);
Corentin Chary27c136c2010-11-29 08:14:05 +01001017}
1018
1019static void eeepc_wmi_platform_exit(struct eeepc_wmi *eeepc)
1020{
1021 eeepc_wmi_sysfs_exit(eeepc->platform_device);
Corentin Chary27c136c2010-11-29 08:14:05 +01001022}
1023
1024/*
Corentin Chary8c1b2d82010-11-29 08:14:09 +01001025 * debugfs
1026 */
1027struct eeepc_wmi_debugfs_node {
1028 struct eeepc_wmi *eeepc;
1029 char *name;
1030 int (*show)(struct seq_file *m, void *data);
1031};
1032
1033static int show_dsts(struct seq_file *m, void *data)
1034{
1035 struct eeepc_wmi *eeepc = m->private;
1036 acpi_status status;
1037 u32 retval = -1;
1038
1039 status = eeepc_wmi_get_devstate(eeepc->debug.dev_id, &retval);
1040
1041 if (ACPI_FAILURE(status))
1042 return -EIO;
1043
1044 seq_printf(m, "DSTS(%x) = %x\n", eeepc->debug.dev_id, retval);
1045
1046 return 0;
1047}
1048
1049static int show_devs(struct seq_file *m, void *data)
1050{
1051 struct eeepc_wmi *eeepc = m->private;
1052 acpi_status status;
1053 u32 retval = -1;
1054
1055 status = eeepc_wmi_set_devstate(eeepc->debug.dev_id,
1056 eeepc->debug.ctrl_param, &retval);
1057 if (ACPI_FAILURE(status))
1058 return -EIO;
1059
1060 seq_printf(m, "DEVS(%x, %x) = %x\n", eeepc->debug.dev_id,
1061 eeepc->debug.ctrl_param, retval);
1062
1063 return 0;
1064}
1065
1066static struct eeepc_wmi_debugfs_node eeepc_wmi_debug_files[] = {
1067 { NULL, "devs", show_devs },
1068 { NULL, "dsts", show_dsts },
1069};
1070
1071static int eeepc_wmi_debugfs_open(struct inode *inode, struct file *file)
1072{
1073 struct eeepc_wmi_debugfs_node *node = inode->i_private;
1074
1075 return single_open(file, node->show, node->eeepc);
1076}
1077
1078static const struct file_operations eeepc_wmi_debugfs_io_ops = {
1079 .owner = THIS_MODULE,
1080 .open = eeepc_wmi_debugfs_open,
1081 .read = seq_read,
1082 .llseek = seq_lseek,
1083 .release = single_release,
1084};
1085
1086static void eeepc_wmi_debugfs_exit(struct eeepc_wmi *eeepc)
1087{
1088 debugfs_remove_recursive(eeepc->debug.root);
1089}
1090
1091static int eeepc_wmi_debugfs_init(struct eeepc_wmi *eeepc)
1092{
1093 struct dentry *dent;
1094 int i;
1095
1096 eeepc->debug.root = debugfs_create_dir(EEEPC_WMI_FILE, NULL);
1097 if (!eeepc->debug.root) {
1098 pr_err("failed to create debugfs directory");
1099 goto error_debugfs;
1100 }
1101
1102 dent = debugfs_create_x32("dev_id", S_IRUGO|S_IWUSR,
1103 eeepc->debug.root, &eeepc->debug.dev_id);
1104 if (!dent)
1105 goto error_debugfs;
1106
1107 dent = debugfs_create_x32("ctrl_param", S_IRUGO|S_IWUSR,
1108 eeepc->debug.root, &eeepc->debug.ctrl_param);
1109 if (!dent)
1110 goto error_debugfs;
1111
1112 for (i = 0; i < ARRAY_SIZE(eeepc_wmi_debug_files); i++) {
1113 struct eeepc_wmi_debugfs_node *node = &eeepc_wmi_debug_files[i];
1114
1115 node->eeepc = eeepc;
1116 dent = debugfs_create_file(node->name, S_IFREG | S_IRUGO,
1117 eeepc->debug.root, node,
1118 &eeepc_wmi_debugfs_io_ops);
1119 if (!dent) {
1120 pr_err("failed to create debug file: %s\n", node->name);
1121 goto error_debugfs;
1122 }
1123 }
1124
1125 return 0;
1126
1127error_debugfs:
1128 eeepc_wmi_debugfs_exit(eeepc);
1129 return -ENOMEM;
1130}
1131
1132/*
Corentin Chary27c136c2010-11-29 08:14:05 +01001133 * WMI Driver
1134 */
Corentin Charyafa7c882011-02-06 13:28:28 +01001135static void eeepc_dmi_check(struct eeepc_wmi *eeepc)
1136{
1137 const char *model;
1138
1139 model = dmi_get_system_info(DMI_PRODUCT_NAME);
1140 if (!model)
1141 return;
1142
1143 /*
1144 * Whitelist for wlan hotplug
1145 *
1146 * Eeepc 1000H needs the current hotplug code to handle
1147 * Fn+F2 correctly. We may add other Eeepc here later, but
1148 * it seems that most of the laptops supported by eeepc-wmi
1149 * don't need to be on this list
1150 */
1151 if (strcmp(model, "1000H") == 0) {
1152 eeepc->hotplug_wireless = true;
1153 pr_info("wlan hotplug enabled\n");
1154 }
1155}
1156
Corentin Charya04ce292011-02-06 13:28:33 +01001157static int __init eeepc_wmi_add(struct platform_device *pdev)
Yong Wangee027e42010-03-21 10:26:34 +08001158{
Yong Wang45f2c692010-04-11 09:27:19 +08001159 struct eeepc_wmi *eeepc;
Yong Wangee027e42010-03-21 10:26:34 +08001160 acpi_status status;
Corentin Chary27c136c2010-11-29 08:14:05 +01001161 int err;
Yong Wangee027e42010-03-21 10:26:34 +08001162
Corentin Chary27c136c2010-11-29 08:14:05 +01001163 eeepc = kzalloc(sizeof(struct eeepc_wmi), GFP_KERNEL);
1164 if (!eeepc)
Corentin Charya04ce292011-02-06 13:28:33 +01001165 return -ENOMEM;
1166
1167 eeepc->platform_device = pdev;
1168 platform_set_drvdata(eeepc->platform_device, eeepc);
Corentin Chary27c136c2010-11-29 08:14:05 +01001169
Corentin Charyafa7c882011-02-06 13:28:28 +01001170 eeepc->hotplug_wireless = hotplug_wireless;
1171 eeepc_dmi_check(eeepc);
1172
Corentin Chary27c136c2010-11-29 08:14:05 +01001173 err = eeepc_wmi_platform_init(eeepc);
1174 if (err)
1175 goto fail_platform;
Yong Wang45f2c692010-04-11 09:27:19 +08001176
1177 err = eeepc_wmi_input_init(eeepc);
1178 if (err)
Corentin Chary27c136c2010-11-29 08:14:05 +01001179 goto fail_input;
Yong Wang3d7b1652010-04-11 09:27:54 +08001180
Corentin Chary084fca62010-11-29 08:14:06 +01001181 err = eeepc_wmi_led_init(eeepc);
1182 if (err)
1183 goto fail_leds;
1184
Corentin Charyba48fdb2010-11-29 08:14:07 +01001185 err = eeepc_wmi_rfkill_init(eeepc);
1186 if (err)
1187 goto fail_rfkill;
1188
Yong Wang3d7b1652010-04-11 09:27:54 +08001189 if (!acpi_video_backlight_support()) {
1190 err = eeepc_wmi_backlight_init(eeepc);
1191 if (err)
Corentin Chary27c136c2010-11-29 08:14:05 +01001192 goto fail_backlight;
Yong Wang3d7b1652010-04-11 09:27:54 +08001193 } else
1194 pr_info("Backlight controlled by ACPI video driver\n");
Yong Wang45f2c692010-04-11 09:27:19 +08001195
1196 status = wmi_install_notify_handler(EEEPC_WMI_EVENT_GUID,
Corentin Chary27c136c2010-11-29 08:14:05 +01001197 eeepc_wmi_notify, eeepc);
Yong Wang45f2c692010-04-11 09:27:19 +08001198 if (ACPI_FAILURE(status)) {
1199 pr_err("Unable to register notify handler - %d\n",
1200 status);
1201 err = -ENODEV;
Corentin Chary27c136c2010-11-29 08:14:05 +01001202 goto fail_wmi_handler;
Yong Wang45f2c692010-04-11 09:27:19 +08001203 }
1204
Corentin Chary8c1b2d82010-11-29 08:14:09 +01001205 err = eeepc_wmi_debugfs_init(eeepc);
1206 if (err)
1207 goto fail_debugfs;
1208
Corentin Charya04ce292011-02-06 13:28:33 +01001209 return 0;
Yong Wang45f2c692010-04-11 09:27:19 +08001210
Corentin Chary8c1b2d82010-11-29 08:14:09 +01001211fail_debugfs:
1212 wmi_remove_notify_handler(EEEPC_WMI_EVENT_GUID);
Corentin Chary27c136c2010-11-29 08:14:05 +01001213fail_wmi_handler:
Yong Wang3d7b1652010-04-11 09:27:54 +08001214 eeepc_wmi_backlight_exit(eeepc);
Corentin Chary27c136c2010-11-29 08:14:05 +01001215fail_backlight:
Corentin Charyba48fdb2010-11-29 08:14:07 +01001216 eeepc_wmi_rfkill_exit(eeepc);
1217fail_rfkill:
Corentin Chary084fca62010-11-29 08:14:06 +01001218 eeepc_wmi_led_exit(eeepc);
1219fail_leds:
Yong Wang45f2c692010-04-11 09:27:19 +08001220 eeepc_wmi_input_exit(eeepc);
Corentin Chary27c136c2010-11-29 08:14:05 +01001221fail_input:
1222 eeepc_wmi_platform_exit(eeepc);
1223fail_platform:
1224 kfree(eeepc);
Corentin Charya04ce292011-02-06 13:28:33 +01001225 return err;
Yong Wang45f2c692010-04-11 09:27:19 +08001226}
1227
Corentin Charya04ce292011-02-06 13:28:33 +01001228static int __exit eeepc_wmi_remove(struct platform_device *device)
Yong Wang45f2c692010-04-11 09:27:19 +08001229{
1230 struct eeepc_wmi *eeepc;
1231
1232 eeepc = platform_get_drvdata(device);
1233 wmi_remove_notify_handler(EEEPC_WMI_EVENT_GUID);
Yong Wang3d7b1652010-04-11 09:27:54 +08001234 eeepc_wmi_backlight_exit(eeepc);
Yong Wang45f2c692010-04-11 09:27:19 +08001235 eeepc_wmi_input_exit(eeepc);
Corentin Chary084fca62010-11-29 08:14:06 +01001236 eeepc_wmi_led_exit(eeepc);
Corentin Charyba48fdb2010-11-29 08:14:07 +01001237 eeepc_wmi_rfkill_exit(eeepc);
Corentin Chary8c1b2d82010-11-29 08:14:09 +01001238 eeepc_wmi_debugfs_exit(eeepc);
Corentin Chary27c136c2010-11-29 08:14:05 +01001239 eeepc_wmi_platform_exit(eeepc);
Yong Wang45f2c692010-04-11 09:27:19 +08001240
Corentin Chary27c136c2010-11-29 08:14:05 +01001241 kfree(eeepc);
Yong Wang45f2c692010-04-11 09:27:19 +08001242 return 0;
1243}
1244
Corentin Chary0773d7f2011-02-06 13:28:32 +01001245/*
1246 * Platform driver - hibernate/resume callbacks
1247 */
1248static int eeepc_hotk_thaw(struct device *device)
1249{
1250 struct eeepc_wmi *eeepc = dev_get_drvdata(device);
1251
1252 if (eeepc->wlan_rfkill) {
1253 bool wlan;
1254
1255 /*
1256 * Work around bios bug - acpi _PTS turns off the wireless led
1257 * during suspend. Normally it restores it on resume, but
1258 * we should kick it ourselves in case hibernation is aborted.
1259 */
1260 wlan = eeepc_wmi_get_devstate_simple(EEEPC_WMI_DEVID_WLAN);
1261 eeepc_wmi_set_devstate(EEEPC_WMI_DEVID_WLAN, wlan, NULL);
1262 }
1263
1264 return 0;
1265}
1266
1267static int eeepc_hotk_restore(struct device *device)
1268{
1269 struct eeepc_wmi *eeepc = dev_get_drvdata(device);
1270 int bl;
1271
1272 /* Refresh both wlan rfkill state and pci hotplug */
1273 if (eeepc->wlan_rfkill)
1274 eeepc_rfkill_hotplug(eeepc);
1275
1276 if (eeepc->bluetooth_rfkill) {
1277 bl = !eeepc_wmi_get_devstate_simple(EEEPC_WMI_DEVID_BLUETOOTH);
1278 rfkill_set_sw_state(eeepc->bluetooth_rfkill, bl);
1279}
1280 if (eeepc->wwan3g_rfkill) {
1281 bl = !eeepc_wmi_get_devstate_simple(EEEPC_WMI_DEVID_WWAN3G);
1282 rfkill_set_sw_state(eeepc->wwan3g_rfkill, bl);
1283 }
1284
1285 return 0;
1286}
1287
1288static const struct dev_pm_ops eeepc_pm_ops = {
1289 .thaw = eeepc_hotk_thaw,
1290 .restore = eeepc_hotk_restore,
1291};
1292
Yong Wang45f2c692010-04-11 09:27:19 +08001293static struct platform_driver platform_driver = {
Corentin Charya04ce292011-02-06 13:28:33 +01001294 .remove = __exit_p(eeepc_wmi_remove),
Yong Wang45f2c692010-04-11 09:27:19 +08001295 .driver = {
1296 .name = EEEPC_WMI_FILE,
1297 .owner = THIS_MODULE,
Corentin Chary0773d7f2011-02-06 13:28:32 +01001298 .pm = &eeepc_pm_ops,
Yong Wang45f2c692010-04-11 09:27:19 +08001299 },
Yong Wang45f2c692010-04-11 09:27:19 +08001300};
1301
Corentin Charyd358cb52010-11-29 08:14:14 +01001302static acpi_status __init eeepc_wmi_parse_device(acpi_handle handle, u32 level,
1303 void *context, void **retval)
1304{
1305 pr_warning("Found legacy ATKD device (%s)", EEEPC_ACPI_HID);
1306 *(bool *)context = true;
1307 return AE_CTRL_TERMINATE;
1308}
1309
1310static int __init eeepc_wmi_check_atkd(void)
1311{
1312 acpi_status status;
1313 bool found = false;
1314
1315 status = acpi_get_devices(EEEPC_ACPI_HID, eeepc_wmi_parse_device,
1316 &found, NULL);
1317
1318 if (ACPI_FAILURE(status) || !found)
1319 return 0;
1320 return -1;
1321}
1322
Corentin Charya04ce292011-02-06 13:28:33 +01001323static int __init eeepc_wmi_probe(struct platform_device *pdev)
Yong Wang45f2c692010-04-11 09:27:19 +08001324{
Yong Wang3d7b1652010-04-11 09:27:54 +08001325 if (!wmi_has_guid(EEEPC_WMI_EVENT_GUID) ||
1326 !wmi_has_guid(EEEPC_WMI_MGMT_GUID)) {
Yong Wang81248882010-04-11 09:26:33 +08001327 pr_warning("No known WMI GUID found\n");
Yong Wangee027e42010-03-21 10:26:34 +08001328 return -ENODEV;
1329 }
1330
Corentin Charyd358cb52010-11-29 08:14:14 +01001331 if (eeepc_wmi_check_atkd()) {
1332 pr_warning("WMI device present, but legacy ATKD device is also "
1333 "present and enabled.");
1334 pr_warning("You probably booted with acpi_osi=\"Linux\" or "
1335 "acpi_osi=\"!Windows 2009\"");
1336 pr_warning("Can't load eeepc-wmi, use default acpi_osi "
1337 "(preferred) or eeepc-laptop");
1338 return -ENODEV;
1339 }
1340
Corentin Charya04ce292011-02-06 13:28:33 +01001341 return eeepc_wmi_add(pdev);
1342}
Yong Wangee027e42010-03-21 10:26:34 +08001343
Corentin Charya04ce292011-02-06 13:28:33 +01001344static struct platform_device *platform_device;
Yong Wangee027e42010-03-21 10:26:34 +08001345
Corentin Charya04ce292011-02-06 13:28:33 +01001346static int __init eeepc_wmi_init(void)
1347{
1348 platform_device = platform_create_bundle(&platform_driver,
1349 eeepc_wmi_probe,
1350 NULL, 0, NULL, 0);
1351 if (IS_ERR(platform_device))
1352 return PTR_ERR(platform_device);
Yong Wangee027e42010-03-21 10:26:34 +08001353 return 0;
1354}
1355
1356static void __exit eeepc_wmi_exit(void)
1357{
Corentin Charya04ce292011-02-06 13:28:33 +01001358 platform_device_unregister(platform_device);
Yong Wang45f2c692010-04-11 09:27:19 +08001359 platform_driver_unregister(&platform_driver);
Yong Wangee027e42010-03-21 10:26:34 +08001360}
1361
1362module_init(eeepc_wmi_init);
1363module_exit(eeepc_wmi_exit);