blob: 910eb00fb13ef5093302b7f2ef6d2e135ba3de06 [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 Charyb7187262011-02-06 13:28:39 +010076#define EEEPC_WMI_DEVID_BACKLIGHT 0x00050011
77#define EEEPC_WMI_DEVID_BRIGHTNESS 0x00050012
Corentin Chary9e1565b2011-02-06 13:28:36 +010078#define EEEPC_WMI_DEVID_CAMERA 0x00060013
79#define EEEPC_WMI_DEVID_CARDREADER 0x00080013
Corentin Chary33e0e6f2011-02-06 13:28:34 +010080#define EEEPC_WMI_DEVID_TPDLED 0x00100011
Yong Wang3d7b1652010-04-11 09:27:54 +080081
Corentin Charyaafa7192011-02-06 13:28:35 +010082#define EEEPC_WMI_DSTS_STATUS_BIT 0x00000001
83#define EEEPC_WMI_DSTS_PRESENCE_BIT 0x00010000
Corentin Charyb7187262011-02-06 13:28:39 +010084#define EEEPC_WMI_DSTS_BRIGHTNESS_MASK 0x000000FF
85#define EEEPC_WMI_DSTS_MAX_BRIGTH_MASK 0x0000FF00
Corentin Charyaafa7192011-02-06 13:28:35 +010086
Corentin Charyafa7c882011-02-06 13:28:28 +010087static bool hotplug_wireless;
88
89module_param(hotplug_wireless, bool, 0444);
90MODULE_PARM_DESC(hotplug_wireless,
91 "Enable hotplug for wireless device. "
92 "If your laptop needs that, please report to "
93 "acpi4asus-user@lists.sourceforge.net.");
94
Yong Wangee027e42010-03-21 10:26:34 +080095static const struct key_entry eeepc_wmi_keymap[] = {
96 /* Sleep already handled via generic ACPI code */
Yong Wangee027e42010-03-21 10:26:34 +080097 { KE_IGNORE, NOTIFY_BRNDOWN_MIN, { KEY_BRIGHTNESSDOWN } },
98 { KE_IGNORE, NOTIFY_BRNUP_MIN, { KEY_BRIGHTNESSUP } },
Corentin Chary5628e5a2011-02-06 13:28:26 +010099 { KE_KEY, 0x30, { KEY_VOLUMEUP } },
100 { KE_KEY, 0x31, { KEY_VOLUMEDOWN } },
101 { KE_KEY, 0x32, { KEY_MUTE } },
102 { KE_KEY, 0x5c, { KEY_F15 } },
103 { KE_KEY, 0x5d, { KEY_WLAN } },
Chris Bagwelleda17482010-10-11 18:47:17 -0500104 { KE_KEY, 0x6b, { KEY_F13 } }, /* Disable Touchpad */
Corentin Charybc40cce2011-02-06 13:28:27 +0100105 { KE_KEY, 0x88, { KEY_WLAN } },
Corentin Chary5628e5a2011-02-06 13:28:26 +0100106 { KE_KEY, 0xcc, { KEY_SWITCHVIDEOMODE } },
107 { KE_KEY, 0xe0, { KEY_PROG1 } },
Chris Bagwelleda17482010-10-11 18:47:17 -0500108 { KE_KEY, 0xe1, { KEY_F14 } },
Corentin Chary54c799a2011-02-06 13:28:38 +0100109 { KE_KEY, 0xe9, { KEY_BRIGHTNESS_ZERO } },
Yong Wangee027e42010-03-21 10:26:34 +0800110 { KE_END, 0},
111};
112
Yong Wang3d7b1652010-04-11 09:27:54 +0800113struct bios_args {
114 u32 dev_id;
115 u32 ctrl_param;
116};
117
Corentin Chary8c1b2d82010-11-29 08:14:09 +0100118/*
119 * eeepc-wmi/ - debugfs root directory
120 * dev_id - current dev_id
121 * ctrl_param - current ctrl_param
122 * devs - call DEVS(dev_id, ctrl_param) and print result
123 * dsts - call DSTS(dev_id) and print result
124 */
125struct eeepc_wmi_debug {
126 struct dentry *root;
127 u32 dev_id;
128 u32 ctrl_param;
129};
130
Yong Wang81248882010-04-11 09:26:33 +0800131struct eeepc_wmi {
Corentin Charyafa7c882011-02-06 13:28:28 +0100132 bool hotplug_wireless;
133
Yong Wang81248882010-04-11 09:26:33 +0800134 struct input_dev *inputdev;
Yong Wang3d7b1652010-04-11 09:27:54 +0800135 struct backlight_device *backlight_device;
Corentin Chary27c136c2010-11-29 08:14:05 +0100136 struct platform_device *platform_device;
Corentin Chary084fca62010-11-29 08:14:06 +0100137
138 struct led_classdev tpd_led;
139 int tpd_led_wk;
140 struct workqueue_struct *led_workqueue;
141 struct work_struct tpd_led_work;
Corentin Charyba48fdb2010-11-29 08:14:07 +0100142
143 struct rfkill *wlan_rfkill;
144 struct rfkill *bluetooth_rfkill;
Corentin Chary2e9e1592011-02-06 13:28:37 +0100145 struct rfkill *wimax_rfkill;
Corentin Charyba48fdb2010-11-29 08:14:07 +0100146 struct rfkill *wwan3g_rfkill;
Corentin Chary8c1b2d82010-11-29 08:14:09 +0100147
Corentin Charyafa7c882011-02-06 13:28:28 +0100148 struct hotplug_slot *hotplug_slot;
149 struct mutex hotplug_lock;
Corentin Chary279f8f92011-02-06 13:28:29 +0100150 struct mutex wmi_lock;
151 struct workqueue_struct *hotplug_workqueue;
152 struct work_struct hotplug_work;
Corentin Charyafa7c882011-02-06 13:28:28 +0100153
Corentin Chary8c1b2d82010-11-29 08:14:09 +0100154 struct eeepc_wmi_debug debug;
Yong Wang81248882010-04-11 09:26:33 +0800155};
156
Yong Wang81248882010-04-11 09:26:33 +0800157static int eeepc_wmi_input_init(struct eeepc_wmi *eeepc)
Yong Wangee027e42010-03-21 10:26:34 +0800158{
159 int err;
160
Yong Wang81248882010-04-11 09:26:33 +0800161 eeepc->inputdev = input_allocate_device();
162 if (!eeepc->inputdev)
Yong Wangee027e42010-03-21 10:26:34 +0800163 return -ENOMEM;
164
Yong Wang81248882010-04-11 09:26:33 +0800165 eeepc->inputdev->name = "Eee PC WMI hotkeys";
Yong Wang45f2c692010-04-11 09:27:19 +0800166 eeepc->inputdev->phys = EEEPC_WMI_FILE "/input0";
Yong Wang81248882010-04-11 09:26:33 +0800167 eeepc->inputdev->id.bustype = BUS_HOST;
Corentin Chary27c136c2010-11-29 08:14:05 +0100168 eeepc->inputdev->dev.parent = &eeepc->platform_device->dev;
Yong Wangee027e42010-03-21 10:26:34 +0800169
Yong Wang81248882010-04-11 09:26:33 +0800170 err = sparse_keymap_setup(eeepc->inputdev, eeepc_wmi_keymap, NULL);
Yong Wangee027e42010-03-21 10:26:34 +0800171 if (err)
172 goto err_free_dev;
173
Yong Wang81248882010-04-11 09:26:33 +0800174 err = input_register_device(eeepc->inputdev);
Yong Wangee027e42010-03-21 10:26:34 +0800175 if (err)
176 goto err_free_keymap;
177
178 return 0;
179
180err_free_keymap:
Yong Wang81248882010-04-11 09:26:33 +0800181 sparse_keymap_free(eeepc->inputdev);
Yong Wangee027e42010-03-21 10:26:34 +0800182err_free_dev:
Yong Wang81248882010-04-11 09:26:33 +0800183 input_free_device(eeepc->inputdev);
Yong Wangee027e42010-03-21 10:26:34 +0800184 return err;
185}
186
Yong Wang81248882010-04-11 09:26:33 +0800187static void eeepc_wmi_input_exit(struct eeepc_wmi *eeepc)
188{
189 if (eeepc->inputdev) {
190 sparse_keymap_free(eeepc->inputdev);
191 input_unregister_device(eeepc->inputdev);
192 }
193
194 eeepc->inputdev = NULL;
195}
196
Corentin Chary2a3f0062010-11-29 08:14:10 +0100197static acpi_status eeepc_wmi_get_devstate(u32 dev_id, u32 *retval)
Yong Wang3d7b1652010-04-11 09:27:54 +0800198{
199 struct acpi_buffer input = { (acpi_size)sizeof(u32), &dev_id };
200 struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER, NULL };
201 union acpi_object *obj;
202 acpi_status status;
203 u32 tmp;
204
205 status = wmi_evaluate_method(EEEPC_WMI_MGMT_GUID,
Corentin Charyafa7c882011-02-06 13:28:28 +0100206 1, EEEPC_WMI_METHODID_DSTS,
207 &input, &output);
Yong Wang3d7b1652010-04-11 09:27:54 +0800208
209 if (ACPI_FAILURE(status))
210 return status;
211
212 obj = (union acpi_object *)output.pointer;
213 if (obj && obj->type == ACPI_TYPE_INTEGER)
214 tmp = (u32)obj->integer.value;
215 else
216 tmp = 0;
217
Corentin Chary2a3f0062010-11-29 08:14:10 +0100218 if (retval)
219 *retval = tmp;
Yong Wang3d7b1652010-04-11 09:27:54 +0800220
221 kfree(obj);
222
223 return status;
224
225}
226
Corentin Chary8c1b2d82010-11-29 08:14:09 +0100227static acpi_status eeepc_wmi_set_devstate(u32 dev_id, u32 ctrl_param,
228 u32 *retval)
Yong Wang3d7b1652010-04-11 09:27:54 +0800229{
230 struct bios_args args = {
231 .dev_id = dev_id,
232 .ctrl_param = ctrl_param,
233 };
234 struct acpi_buffer input = { (acpi_size)sizeof(args), &args };
235 acpi_status status;
236
Corentin Chary8c1b2d82010-11-29 08:14:09 +0100237 if (!retval) {
238 status = wmi_evaluate_method(EEEPC_WMI_MGMT_GUID, 1,
239 EEEPC_WMI_METHODID_DEVS,
240 &input, NULL);
241 } else {
242 struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER, NULL };
243 union acpi_object *obj;
244 u32 tmp;
245
246 status = wmi_evaluate_method(EEEPC_WMI_MGMT_GUID, 1,
247 EEEPC_WMI_METHODID_DEVS,
248 &input, &output);
249
250 if (ACPI_FAILURE(status))
251 return status;
252
253 obj = (union acpi_object *)output.pointer;
254 if (obj && obj->type == ACPI_TYPE_INTEGER)
255 tmp = (u32)obj->integer.value;
256 else
257 tmp = 0;
258
259 *retval = tmp;
260
261 kfree(obj);
262 }
Yong Wang3d7b1652010-04-11 09:27:54 +0800263
264 return status;
265}
266
Corentin Chary5c956382011-02-06 13:28:31 +0100267/* Helper for special devices with magic return codes */
Corentin Charyb7187262011-02-06 13:28:39 +0100268static int eeepc_wmi_get_devstate_bits(u32 dev_id, u32 mask)
Corentin Chary5c956382011-02-06 13:28:31 +0100269{
270 u32 retval = 0;
271 acpi_status status;
272
273 status = eeepc_wmi_get_devstate(dev_id, &retval);
274
275 if (ACPI_FAILURE(status))
276 return -EINVAL;
277
Corentin Charyaafa7192011-02-06 13:28:35 +0100278 if (!(retval & EEEPC_WMI_DSTS_PRESENCE_BIT))
Corentin Chary5c956382011-02-06 13:28:31 +0100279 return -ENODEV;
280
Corentin Charyb7187262011-02-06 13:28:39 +0100281 return retval & mask;
282}
283
284static int eeepc_wmi_get_devstate_simple(u32 dev_id)
285{
286 return eeepc_wmi_get_devstate_bits(dev_id, EEEPC_WMI_DSTS_STATUS_BIT);
Corentin Chary5c956382011-02-06 13:28:31 +0100287}
288
Corentin Chary084fca62010-11-29 08:14:06 +0100289/*
290 * LEDs
291 */
292/*
293 * These functions actually update the LED's, and are called from a
294 * workqueue. By doing this as separate work rather than when the LED
295 * subsystem asks, we avoid messing with the Eeepc ACPI stuff during a
296 * potentially bad time, such as a timer interrupt.
297 */
298static void tpd_led_update(struct work_struct *work)
299{
300 int ctrl_param;
301 struct eeepc_wmi *eeepc;
302
303 eeepc = container_of(work, struct eeepc_wmi, tpd_led_work);
304
305 ctrl_param = eeepc->tpd_led_wk;
Corentin Chary8c1b2d82010-11-29 08:14:09 +0100306 eeepc_wmi_set_devstate(EEEPC_WMI_DEVID_TPDLED, ctrl_param, NULL);
Corentin Chary084fca62010-11-29 08:14:06 +0100307}
308
309static void tpd_led_set(struct led_classdev *led_cdev,
310 enum led_brightness value)
311{
312 struct eeepc_wmi *eeepc;
313
314 eeepc = container_of(led_cdev, struct eeepc_wmi, tpd_led);
315
316 eeepc->tpd_led_wk = !!value;
317 queue_work(eeepc->led_workqueue, &eeepc->tpd_led_work);
318}
319
320static int read_tpd_state(struct eeepc_wmi *eeepc)
321{
Corentin Chary5c956382011-02-06 13:28:31 +0100322 return eeepc_wmi_get_devstate_simple(EEEPC_WMI_DEVID_TPDLED);
Corentin Chary084fca62010-11-29 08:14:06 +0100323}
324
325static enum led_brightness tpd_led_get(struct led_classdev *led_cdev)
326{
327 struct eeepc_wmi *eeepc;
328
329 eeepc = container_of(led_cdev, struct eeepc_wmi, tpd_led);
330
331 return read_tpd_state(eeepc);
332}
333
334static int eeepc_wmi_led_init(struct eeepc_wmi *eeepc)
335{
336 int rv;
337
338 if (read_tpd_state(eeepc) < 0)
339 return 0;
340
341 eeepc->led_workqueue = create_singlethread_workqueue("led_workqueue");
342 if (!eeepc->led_workqueue)
343 return -ENOMEM;
344 INIT_WORK(&eeepc->tpd_led_work, tpd_led_update);
345
346 eeepc->tpd_led.name = "eeepc::touchpad";
347 eeepc->tpd_led.brightness_set = tpd_led_set;
348 eeepc->tpd_led.brightness_get = tpd_led_get;
349 eeepc->tpd_led.max_brightness = 1;
350
351 rv = led_classdev_register(&eeepc->platform_device->dev,
352 &eeepc->tpd_led);
353 if (rv) {
354 destroy_workqueue(eeepc->led_workqueue);
355 return rv;
356 }
357
358 return 0;
359}
360
361static void eeepc_wmi_led_exit(struct eeepc_wmi *eeepc)
362{
363 if (eeepc->tpd_led.dev)
364 led_classdev_unregister(&eeepc->tpd_led);
365 if (eeepc->led_workqueue)
366 destroy_workqueue(eeepc->led_workqueue);
367}
368
369/*
Corentin Charyafa7c882011-02-06 13:28:28 +0100370 * PCI hotplug (for wlan rfkill)
371 */
372static bool eeepc_wlan_rfkill_blocked(struct eeepc_wmi *eeepc)
373{
Corentin Chary5c956382011-02-06 13:28:31 +0100374 int result = eeepc_wmi_get_devstate_simple(EEEPC_WMI_DEVID_WLAN);
Corentin Charyafa7c882011-02-06 13:28:28 +0100375
Corentin Chary5c956382011-02-06 13:28:31 +0100376 if (result < 0)
Corentin Charyafa7c882011-02-06 13:28:28 +0100377 return false;
Corentin Chary5c956382011-02-06 13:28:31 +0100378 return !result;
Corentin Charyafa7c882011-02-06 13:28:28 +0100379}
380
381static void eeepc_rfkill_hotplug(struct eeepc_wmi *eeepc)
382{
383 struct pci_dev *dev;
384 struct pci_bus *bus;
Corentin Chary279f8f92011-02-06 13:28:29 +0100385 bool blocked;
Corentin Charyafa7c882011-02-06 13:28:28 +0100386 bool absent;
387 u32 l;
388
Corentin Chary279f8f92011-02-06 13:28:29 +0100389 mutex_lock(&eeepc->wmi_lock);
390 blocked = eeepc_wlan_rfkill_blocked(eeepc);
391 mutex_unlock(&eeepc->wmi_lock);
Corentin Charyafa7c882011-02-06 13:28:28 +0100392
393 mutex_lock(&eeepc->hotplug_lock);
394
Corentin Chary279f8f92011-02-06 13:28:29 +0100395 if (eeepc->wlan_rfkill)
396 rfkill_set_sw_state(eeepc->wlan_rfkill, blocked);
397
Corentin Charyafa7c882011-02-06 13:28:28 +0100398 if (eeepc->hotplug_slot) {
399 bus = pci_find_bus(0, 1);
400 if (!bus) {
401 pr_warning("Unable to find PCI bus 1?\n");
402 goto out_unlock;
403 }
404
405 if (pci_bus_read_config_dword(bus, 0, PCI_VENDOR_ID, &l)) {
406 pr_err("Unable to read PCI config space?\n");
407 goto out_unlock;
408 }
409 absent = (l == 0xffffffff);
410
411 if (blocked != absent) {
412 pr_warning("BIOS says wireless lan is %s, "
413 "but the pci device is %s\n",
414 blocked ? "blocked" : "unblocked",
415 absent ? "absent" : "present");
416 pr_warning("skipped wireless hotplug as probably "
417 "inappropriate for this model\n");
418 goto out_unlock;
419 }
420
421 if (!blocked) {
422 dev = pci_get_slot(bus, 0);
423 if (dev) {
424 /* Device already present */
425 pci_dev_put(dev);
426 goto out_unlock;
427 }
428 dev = pci_scan_single_device(bus, 0);
429 if (dev) {
430 pci_bus_assign_resources(bus);
431 if (pci_bus_add_device(dev))
432 pr_err("Unable to hotplug wifi\n");
433 }
434 } else {
435 dev = pci_get_slot(bus, 0);
436 if (dev) {
437 pci_remove_bus_device(dev);
438 pci_dev_put(dev);
439 }
440 }
441 }
442
443out_unlock:
444 mutex_unlock(&eeepc->hotplug_lock);
445}
446
447static void eeepc_rfkill_notify(acpi_handle handle, u32 event, void *data)
448{
449 struct eeepc_wmi *eeepc = data;
450
451 if (event != ACPI_NOTIFY_BUS_CHECK)
452 return;
453
Corentin Chary279f8f92011-02-06 13:28:29 +0100454 /*
455 * We can't call directly eeepc_rfkill_hotplug because most
456 * of the time WMBC is still being executed and not reetrant.
457 * There is currently no way to tell ACPICA that we want this
458 * method to be serialized, we schedule a eeepc_rfkill_hotplug
459 * call later, in a safer context.
460 */
461 queue_work(eeepc->hotplug_workqueue, &eeepc->hotplug_work);
Corentin Charyafa7c882011-02-06 13:28:28 +0100462}
463
464static int eeepc_register_rfkill_notifier(struct eeepc_wmi *eeepc,
465 char *node)
466{
467 acpi_status status;
468 acpi_handle handle;
469
470 status = acpi_get_handle(NULL, node, &handle);
471
472 if (ACPI_SUCCESS(status)) {
473 status = acpi_install_notify_handler(handle,
474 ACPI_SYSTEM_NOTIFY,
475 eeepc_rfkill_notify,
476 eeepc);
477 if (ACPI_FAILURE(status))
478 pr_warning("Failed to register notify on %s\n", node);
479 } else
480 return -ENODEV;
481
482 return 0;
483}
484
485static void eeepc_unregister_rfkill_notifier(struct eeepc_wmi *eeepc,
486 char *node)
487{
488 acpi_status status = AE_OK;
489 acpi_handle handle;
490
491 status = acpi_get_handle(NULL, node, &handle);
492
493 if (ACPI_SUCCESS(status)) {
494 status = acpi_remove_notify_handler(handle,
495 ACPI_SYSTEM_NOTIFY,
496 eeepc_rfkill_notify);
497 if (ACPI_FAILURE(status))
498 pr_err("Error removing rfkill notify handler %s\n",
499 node);
500 }
501}
502
503static int eeepc_get_adapter_status(struct hotplug_slot *hotplug_slot,
504 u8 *value)
505{
Corentin Chary5c956382011-02-06 13:28:31 +0100506 int result = eeepc_wmi_get_devstate_simple(EEEPC_WMI_DEVID_WLAN);
Corentin Charyafa7c882011-02-06 13:28:28 +0100507
Corentin Chary5c956382011-02-06 13:28:31 +0100508 if (result < 0)
509 return result;
Corentin Charyafa7c882011-02-06 13:28:28 +0100510
Corentin Chary5c956382011-02-06 13:28:31 +0100511 *value = !!result;
Corentin Charyafa7c882011-02-06 13:28:28 +0100512 return 0;
513}
514
515static void eeepc_cleanup_pci_hotplug(struct hotplug_slot *hotplug_slot)
516{
517 kfree(hotplug_slot->info);
518 kfree(hotplug_slot);
519}
520
521static struct hotplug_slot_ops eeepc_hotplug_slot_ops = {
522 .owner = THIS_MODULE,
523 .get_adapter_status = eeepc_get_adapter_status,
524 .get_power_status = eeepc_get_adapter_status,
525};
526
Corentin Chary279f8f92011-02-06 13:28:29 +0100527static void eeepc_hotplug_work(struct work_struct *work)
528{
529 struct eeepc_wmi *eeepc;
530
531 eeepc = container_of(work, struct eeepc_wmi, hotplug_work);
532 eeepc_rfkill_hotplug(eeepc);
533}
534
Corentin Charyafa7c882011-02-06 13:28:28 +0100535static int eeepc_setup_pci_hotplug(struct eeepc_wmi *eeepc)
536{
537 int ret = -ENOMEM;
538 struct pci_bus *bus = pci_find_bus(0, 1);
539
540 if (!bus) {
541 pr_err("Unable to find wifi PCI bus\n");
542 return -ENODEV;
543 }
544
Corentin Chary279f8f92011-02-06 13:28:29 +0100545 eeepc->hotplug_workqueue =
546 create_singlethread_workqueue("hotplug_workqueue");
547 if (!eeepc->hotplug_workqueue)
548 goto error_workqueue;
549
550 INIT_WORK(&eeepc->hotplug_work, eeepc_hotplug_work);
551
Corentin Charyafa7c882011-02-06 13:28:28 +0100552 eeepc->hotplug_slot = kzalloc(sizeof(struct hotplug_slot), GFP_KERNEL);
553 if (!eeepc->hotplug_slot)
554 goto error_slot;
555
556 eeepc->hotplug_slot->info = kzalloc(sizeof(struct hotplug_slot_info),
557 GFP_KERNEL);
558 if (!eeepc->hotplug_slot->info)
559 goto error_info;
560
561 eeepc->hotplug_slot->private = eeepc;
562 eeepc->hotplug_slot->release = &eeepc_cleanup_pci_hotplug;
563 eeepc->hotplug_slot->ops = &eeepc_hotplug_slot_ops;
564 eeepc_get_adapter_status(eeepc->hotplug_slot,
565 &eeepc->hotplug_slot->info->adapter_status);
566
567 ret = pci_hp_register(eeepc->hotplug_slot, bus, 0, "eeepc-wifi");
568 if (ret) {
569 pr_err("Unable to register hotplug slot - %d\n", ret);
570 goto error_register;
571 }
572
573 return 0;
574
575error_register:
576 kfree(eeepc->hotplug_slot->info);
577error_info:
578 kfree(eeepc->hotplug_slot);
579 eeepc->hotplug_slot = NULL;
580error_slot:
Corentin Chary279f8f92011-02-06 13:28:29 +0100581 destroy_workqueue(eeepc->hotplug_workqueue);
582error_workqueue:
Corentin Charyafa7c882011-02-06 13:28:28 +0100583 return ret;
584}
585
586/*
Corentin Charyba48fdb2010-11-29 08:14:07 +0100587 * Rfkill devices
588 */
589static int eeepc_rfkill_set(void *data, bool blocked)
590{
591 int dev_id = (unsigned long)data;
592 u32 ctrl_param = !blocked;
Corentin Chary7898cf12011-02-06 13:28:30 +0100593 acpi_status status;
Corentin Charyba48fdb2010-11-29 08:14:07 +0100594
Corentin Chary7898cf12011-02-06 13:28:30 +0100595 status = eeepc_wmi_set_devstate(dev_id, ctrl_param, NULL);
596
597 if (ACPI_FAILURE(status))
598 return -EIO;
599
600 return 0;
Corentin Charyba48fdb2010-11-29 08:14:07 +0100601}
602
603static void eeepc_rfkill_query(struct rfkill *rfkill, void *data)
604{
605 int dev_id = (unsigned long)data;
Corentin Chary5c956382011-02-06 13:28:31 +0100606 int result;
Corentin Charyba48fdb2010-11-29 08:14:07 +0100607
Corentin Chary5c956382011-02-06 13:28:31 +0100608 result = eeepc_wmi_get_devstate_simple(dev_id);
Corentin Charyba48fdb2010-11-29 08:14:07 +0100609
Corentin Chary5c956382011-02-06 13:28:31 +0100610 if (result < 0)
Corentin Charyba48fdb2010-11-29 08:14:07 +0100611 return ;
612
Corentin Chary5c956382011-02-06 13:28:31 +0100613 rfkill_set_sw_state(rfkill, !result);
Corentin Charyba48fdb2010-11-29 08:14:07 +0100614}
615
Corentin Chary279f8f92011-02-06 13:28:29 +0100616static int eeepc_rfkill_wlan_set(void *data, bool blocked)
617{
618 struct eeepc_wmi *eeepc = data;
619 int ret;
620
621 /*
622 * This handler is enabled only if hotplug is enabled.
623 * In this case, the eeepc_wmi_set_devstate() will
624 * trigger a wmi notification and we need to wait
625 * this call to finish before being able to call
626 * any wmi method
627 */
628 mutex_lock(&eeepc->wmi_lock);
629 ret = eeepc_rfkill_set((void *)(long)EEEPC_WMI_DEVID_WLAN, blocked);
630 mutex_unlock(&eeepc->wmi_lock);
631 return ret;
632}
633
634static void eeepc_rfkill_wlan_query(struct rfkill *rfkill, void *data)
635{
636 eeepc_rfkill_query(rfkill, (void *)(long)EEEPC_WMI_DEVID_WLAN);
637}
638
639static const struct rfkill_ops eeepc_rfkill_wlan_ops = {
640 .set_block = eeepc_rfkill_wlan_set,
641 .query = eeepc_rfkill_wlan_query,
642};
643
Corentin Charyba48fdb2010-11-29 08:14:07 +0100644static const struct rfkill_ops eeepc_rfkill_ops = {
645 .set_block = eeepc_rfkill_set,
646 .query = eeepc_rfkill_query,
647};
648
649static int eeepc_new_rfkill(struct eeepc_wmi *eeepc,
650 struct rfkill **rfkill,
651 const char *name,
652 enum rfkill_type type, int dev_id)
653{
Corentin Chary5c956382011-02-06 13:28:31 +0100654 int result = eeepc_wmi_get_devstate_simple(dev_id);
Corentin Charyba48fdb2010-11-29 08:14:07 +0100655
Corentin Chary5c956382011-02-06 13:28:31 +0100656 if (result < 0)
657 return result;
Corentin Charyba48fdb2010-11-29 08:14:07 +0100658
Corentin Chary279f8f92011-02-06 13:28:29 +0100659 if (dev_id == EEEPC_WMI_DEVID_WLAN && eeepc->hotplug_wireless)
660 *rfkill = rfkill_alloc(name, &eeepc->platform_device->dev, type,
661 &eeepc_rfkill_wlan_ops, eeepc);
662 else
663 *rfkill = rfkill_alloc(name, &eeepc->platform_device->dev, type,
664 &eeepc_rfkill_ops, (void *)(long)dev_id);
Corentin Charyba48fdb2010-11-29 08:14:07 +0100665
666 if (!*rfkill)
667 return -EINVAL;
668
Corentin Chary5c956382011-02-06 13:28:31 +0100669 rfkill_init_sw_state(*rfkill, !result);
Corentin Charyba48fdb2010-11-29 08:14:07 +0100670 result = rfkill_register(*rfkill);
671 if (result) {
672 rfkill_destroy(*rfkill);
673 *rfkill = NULL;
674 return result;
675 }
676 return 0;
677}
678
679static void eeepc_wmi_rfkill_exit(struct eeepc_wmi *eeepc)
680{
Corentin Charyafa7c882011-02-06 13:28:28 +0100681 eeepc_unregister_rfkill_notifier(eeepc, "\\_SB.PCI0.P0P5");
682 eeepc_unregister_rfkill_notifier(eeepc, "\\_SB.PCI0.P0P6");
683 eeepc_unregister_rfkill_notifier(eeepc, "\\_SB.PCI0.P0P7");
Corentin Charyba48fdb2010-11-29 08:14:07 +0100684 if (eeepc->wlan_rfkill) {
685 rfkill_unregister(eeepc->wlan_rfkill);
686 rfkill_destroy(eeepc->wlan_rfkill);
687 eeepc->wlan_rfkill = NULL;
688 }
Corentin Charyafa7c882011-02-06 13:28:28 +0100689 /*
690 * Refresh pci hotplug in case the rfkill state was changed after
691 * eeepc_unregister_rfkill_notifier()
692 */
693 eeepc_rfkill_hotplug(eeepc);
694 if (eeepc->hotplug_slot)
695 pci_hp_deregister(eeepc->hotplug_slot);
Corentin Chary279f8f92011-02-06 13:28:29 +0100696 if (eeepc->hotplug_workqueue)
697 destroy_workqueue(eeepc->hotplug_workqueue);
Corentin Charyafa7c882011-02-06 13:28:28 +0100698
Corentin Charyba48fdb2010-11-29 08:14:07 +0100699 if (eeepc->bluetooth_rfkill) {
700 rfkill_unregister(eeepc->bluetooth_rfkill);
701 rfkill_destroy(eeepc->bluetooth_rfkill);
702 eeepc->bluetooth_rfkill = NULL;
703 }
Corentin Chary2e9e1592011-02-06 13:28:37 +0100704 if (eeepc->wimax_rfkill) {
705 rfkill_unregister(eeepc->wimax_rfkill);
706 rfkill_destroy(eeepc->wimax_rfkill);
707 eeepc->wimax_rfkill = NULL;
708 }
Corentin Charyba48fdb2010-11-29 08:14:07 +0100709 if (eeepc->wwan3g_rfkill) {
710 rfkill_unregister(eeepc->wwan3g_rfkill);
711 rfkill_destroy(eeepc->wwan3g_rfkill);
712 eeepc->wwan3g_rfkill = NULL;
713 }
714}
715
716static int eeepc_wmi_rfkill_init(struct eeepc_wmi *eeepc)
717{
718 int result = 0;
719
Corentin Charyafa7c882011-02-06 13:28:28 +0100720 mutex_init(&eeepc->hotplug_lock);
Corentin Chary279f8f92011-02-06 13:28:29 +0100721 mutex_init(&eeepc->wmi_lock);
Corentin Charyafa7c882011-02-06 13:28:28 +0100722
Corentin Charyba48fdb2010-11-29 08:14:07 +0100723 result = eeepc_new_rfkill(eeepc, &eeepc->wlan_rfkill,
724 "eeepc-wlan", RFKILL_TYPE_WLAN,
725 EEEPC_WMI_DEVID_WLAN);
726
727 if (result && result != -ENODEV)
728 goto exit;
729
730 result = eeepc_new_rfkill(eeepc, &eeepc->bluetooth_rfkill,
731 "eeepc-bluetooth", RFKILL_TYPE_BLUETOOTH,
732 EEEPC_WMI_DEVID_BLUETOOTH);
733
734 if (result && result != -ENODEV)
735 goto exit;
736
Corentin Chary2e9e1592011-02-06 13:28:37 +0100737 result = eeepc_new_rfkill(eeepc, &eeepc->wimax_rfkill,
738 "eeepc-wimax", RFKILL_TYPE_WIMAX,
739 EEEPC_WMI_DEVID_WIMAX);
740
741 if (result && result != -ENODEV)
742 goto exit;
743
Corentin Charyba48fdb2010-11-29 08:14:07 +0100744 result = eeepc_new_rfkill(eeepc, &eeepc->wwan3g_rfkill,
745 "eeepc-wwan3g", RFKILL_TYPE_WWAN,
746 EEEPC_WMI_DEVID_WWAN3G);
747
748 if (result && result != -ENODEV)
749 goto exit;
750
Corentin Charyafa7c882011-02-06 13:28:28 +0100751 result = eeepc_setup_pci_hotplug(eeepc);
752 /*
753 * If we get -EBUSY then something else is handling the PCI hotplug -
754 * don't fail in this case
755 */
756 if (result == -EBUSY)
757 result = 0;
758
759 eeepc_register_rfkill_notifier(eeepc, "\\_SB.PCI0.P0P5");
760 eeepc_register_rfkill_notifier(eeepc, "\\_SB.PCI0.P0P6");
761 eeepc_register_rfkill_notifier(eeepc, "\\_SB.PCI0.P0P7");
762 /*
763 * Refresh pci hotplug in case the rfkill state was changed during
764 * setup.
765 */
766 eeepc_rfkill_hotplug(eeepc);
767
Corentin Charyba48fdb2010-11-29 08:14:07 +0100768exit:
769 if (result && result != -ENODEV)
770 eeepc_wmi_rfkill_exit(eeepc);
771
772 if (result == -ENODEV)
773 result = 0;
774
775 return result;
776}
777
778/*
Corentin Chary084fca62010-11-29 08:14:06 +0100779 * Backlight
780 */
Corentin Charyb7187262011-02-06 13:28:39 +0100781static int read_backlight_power(void)
782{
783 int ret = eeepc_wmi_get_devstate_simple(EEEPC_WMI_DEVID_BACKLIGHT);
784
785 if (ret < 0)
786 return ret;
787
788 return ret ? FB_BLANK_UNBLANK : FB_BLANK_POWERDOWN;
789}
790
Yong Wang3d7b1652010-04-11 09:27:54 +0800791static int read_brightness(struct backlight_device *bd)
792{
Corentin Charydfed65d2010-11-29 08:14:12 +0100793 u32 retval;
Yong Wang3d7b1652010-04-11 09:27:54 +0800794 acpi_status status;
795
Corentin Charyb7187262011-02-06 13:28:39 +0100796 status = eeepc_wmi_get_devstate(EEEPC_WMI_DEVID_BRIGHTNESS, &retval);
Yong Wang3d7b1652010-04-11 09:27:54 +0800797
798 if (ACPI_FAILURE(status))
Corentin Charyb7187262011-02-06 13:28:39 +0100799 return -EIO;
Yong Wang3d7b1652010-04-11 09:27:54 +0800800 else
Corentin Charyb7187262011-02-06 13:28:39 +0100801 return retval & EEEPC_WMI_DSTS_BRIGHTNESS_MASK;
Yong Wang3d7b1652010-04-11 09:27:54 +0800802}
803
804static int update_bl_status(struct backlight_device *bd)
805{
Corentin Charydfed65d2010-11-29 08:14:12 +0100806 u32 ctrl_param;
Yong Wang3d7b1652010-04-11 09:27:54 +0800807 acpi_status status;
Corentin Charyb7187262011-02-06 13:28:39 +0100808 int power;
Yong Wang3d7b1652010-04-11 09:27:54 +0800809
810 ctrl_param = bd->props.brightness;
811
Corentin Charyb7187262011-02-06 13:28:39 +0100812 status = eeepc_wmi_set_devstate(EEEPC_WMI_DEVID_BRIGHTNESS,
Corentin Chary8c1b2d82010-11-29 08:14:09 +0100813 ctrl_param, NULL);
Yong Wang3d7b1652010-04-11 09:27:54 +0800814
815 if (ACPI_FAILURE(status))
Corentin Charyb7187262011-02-06 13:28:39 +0100816 return -EIO;
817
818 power = read_backlight_power();
819 if (power != -ENODEV && bd->props.power != power) {
820 ctrl_param = !!(bd->props.power == FB_BLANK_UNBLANK);
821 status = eeepc_wmi_set_devstate(EEEPC_WMI_DEVID_BACKLIGHT,
822 ctrl_param, NULL);
823
824 if (ACPI_FAILURE(status))
825 return -EIO;
826 }
827 return 0;
Yong Wang3d7b1652010-04-11 09:27:54 +0800828}
829
830static const struct backlight_ops eeepc_wmi_bl_ops = {
831 .get_brightness = read_brightness,
832 .update_status = update_bl_status,
833};
834
835static int eeepc_wmi_backlight_notify(struct eeepc_wmi *eeepc, int code)
836{
837 struct backlight_device *bd = eeepc->backlight_device;
838 int old = bd->props.brightness;
Daniel Mackb7670ed2010-05-19 12:37:01 +0200839 int new = old;
Yong Wang3d7b1652010-04-11 09:27:54 +0800840
841 if (code >= NOTIFY_BRNUP_MIN && code <= NOTIFY_BRNUP_MAX)
842 new = code - NOTIFY_BRNUP_MIN + 1;
843 else if (code >= NOTIFY_BRNDOWN_MIN && code <= NOTIFY_BRNDOWN_MAX)
844 new = code - NOTIFY_BRNDOWN_MIN;
845
846 bd->props.brightness = new;
847 backlight_update_status(bd);
848 backlight_force_update(bd, BACKLIGHT_UPDATE_HOTKEY);
849
850 return old;
851}
852
853static int eeepc_wmi_backlight_init(struct eeepc_wmi *eeepc)
854{
855 struct backlight_device *bd;
856 struct backlight_properties props;
Corentin Charyb7187262011-02-06 13:28:39 +0100857 int max;
858 int power;
859
860 max = eeepc_wmi_get_devstate_bits(EEEPC_WMI_DEVID_BRIGHTNESS,
861 EEEPC_WMI_DSTS_MAX_BRIGTH_MASK);
862 power = read_backlight_power();
863
864 if (max < 0 && power < 0) {
865 /* Try to keep the original error */
866 if (max == -ENODEV && power == -ENODEV)
867 return -ENODEV;
868 if (max != -ENODEV)
869 return max;
870 else
871 return power;
872 }
873 if (max == -ENODEV)
874 max = 0;
875 if (power == -ENODEV)
876 power = FB_BLANK_UNBLANK;
Yong Wang3d7b1652010-04-11 09:27:54 +0800877
878 memset(&props, 0, sizeof(struct backlight_properties));
Corentin Charyb7187262011-02-06 13:28:39 +0100879 props.max_brightness = max;
Yong Wang3d7b1652010-04-11 09:27:54 +0800880 bd = backlight_device_register(EEEPC_WMI_FILE,
Corentin Chary27c136c2010-11-29 08:14:05 +0100881 &eeepc->platform_device->dev, eeepc,
Yong Wang3d7b1652010-04-11 09:27:54 +0800882 &eeepc_wmi_bl_ops, &props);
883 if (IS_ERR(bd)) {
884 pr_err("Could not register backlight device\n");
885 return PTR_ERR(bd);
886 }
887
888 eeepc->backlight_device = bd;
889
890 bd->props.brightness = read_brightness(bd);
Corentin Charyb7187262011-02-06 13:28:39 +0100891 bd->props.power = power;
Yong Wang3d7b1652010-04-11 09:27:54 +0800892 backlight_update_status(bd);
893
894 return 0;
895}
896
897static void eeepc_wmi_backlight_exit(struct eeepc_wmi *eeepc)
898{
899 if (eeepc->backlight_device)
900 backlight_device_unregister(eeepc->backlight_device);
901
902 eeepc->backlight_device = NULL;
903}
904
905static void eeepc_wmi_notify(u32 value, void *context)
906{
907 struct eeepc_wmi *eeepc = context;
908 struct acpi_buffer response = { ACPI_ALLOCATE_BUFFER, NULL };
909 union acpi_object *obj;
910 acpi_status status;
911 int code;
912 int orig_code;
913
914 status = wmi_get_event_data(value, &response);
915 if (status != AE_OK) {
916 pr_err("bad event status 0x%x\n", status);
917 return;
918 }
919
920 obj = (union acpi_object *)response.pointer;
921
922 if (obj && obj->type == ACPI_TYPE_INTEGER) {
923 code = obj->integer.value;
924 orig_code = code;
925
926 if (code >= NOTIFY_BRNUP_MIN && code <= NOTIFY_BRNUP_MAX)
927 code = NOTIFY_BRNUP_MIN;
928 else if (code >= NOTIFY_BRNDOWN_MIN &&
929 code <= NOTIFY_BRNDOWN_MAX)
930 code = NOTIFY_BRNDOWN_MIN;
931
932 if (code == NOTIFY_BRNUP_MIN || code == NOTIFY_BRNDOWN_MIN) {
933 if (!acpi_video_backlight_support())
934 eeepc_wmi_backlight_notify(eeepc, orig_code);
935 }
936
937 if (!sparse_keymap_report_event(eeepc->inputdev,
938 code, 1, true))
939 pr_info("Unknown key %x pressed\n", code);
940 }
941
942 kfree(obj);
943}
944
Corentin Chary9e1565b2011-02-06 13:28:36 +0100945/*
946 * Sys helpers
947 */
948static int parse_arg(const char *buf, unsigned long count, int *val)
949{
950 if (!count)
951 return 0;
952 if (sscanf(buf, "%i", val) != 1)
953 return -EINVAL;
954 return count;
955}
956
957static ssize_t store_sys_wmi(int devid, const char *buf, size_t count)
958{
959 acpi_status status;
960 u32 retval;
961 int rv, value;
962
963 value = eeepc_wmi_get_devstate_simple(devid);
964 if (value == -ENODEV) /* Check device presence */
965 return value;
966
967 rv = parse_arg(buf, count, &value);
968 status = eeepc_wmi_set_devstate(devid, value, &retval);
969
970 if (ACPI_FAILURE(status))
971 return -EIO;
972 return rv;
973}
974
975static ssize_t show_sys_wmi(int devid, char *buf)
976{
977 int value = eeepc_wmi_get_devstate_simple(devid);
978
979 if (value < 0)
980 return value;
981
982 return sprintf(buf, "%d\n", value);
983}
984
985#define EEEPC_WMI_CREATE_DEVICE_ATTR(_name, _mode, _cm) \
986 static ssize_t show_##_name(struct device *dev, \
987 struct device_attribute *attr, \
988 char *buf) \
989 { \
990 return show_sys_wmi(_cm, buf); \
991 } \
992 static ssize_t store_##_name(struct device *dev, \
993 struct device_attribute *attr, \
994 const char *buf, size_t count) \
995 { \
996 return store_sys_wmi(_cm, buf, count); \
997 } \
998 static struct device_attribute dev_attr_##_name = { \
999 .attr = { \
1000 .name = __stringify(_name), \
1001 .mode = _mode }, \
1002 .show = show_##_name, \
1003 .store = store_##_name, \
1004 }
1005
1006EEEPC_WMI_CREATE_DEVICE_ATTR(camera, 0644, EEEPC_WMI_DEVID_CAMERA);
1007EEEPC_WMI_CREATE_DEVICE_ATTR(cardr, 0644, EEEPC_WMI_DEVID_CARDREADER);
1008
Dmitry Torokhov67fa38e2010-11-03 11:14:01 -07001009static ssize_t store_cpufv(struct device *dev, struct device_attribute *attr,
1010 const char *buf, size_t count)
Chris Bagwell7f80d732010-10-11 18:47:18 -05001011{
1012 int value;
1013 struct acpi_buffer input = { (acpi_size)sizeof(value), &value };
1014 acpi_status status;
1015
1016 if (!count || sscanf(buf, "%i", &value) != 1)
1017 return -EINVAL;
1018 if (value < 0 || value > 2)
1019 return -EINVAL;
1020
1021 status = wmi_evaluate_method(EEEPC_WMI_MGMT_GUID,
1022 1, EEEPC_WMI_METHODID_CFVS, &input, NULL);
1023
1024 if (ACPI_FAILURE(status))
1025 return -EIO;
1026 else
1027 return count;
1028}
1029
1030static DEVICE_ATTR(cpufv, S_IRUGO | S_IWUSR, NULL, store_cpufv);
1031
Corentin Chary4e37b422010-11-29 08:14:08 +01001032static struct attribute *platform_attributes[] = {
1033 &dev_attr_cpufv.attr,
Corentin Chary9e1565b2011-02-06 13:28:36 +01001034 &dev_attr_camera.attr,
1035 &dev_attr_cardr.attr,
Corentin Chary4e37b422010-11-29 08:14:08 +01001036 NULL
1037};
1038
Corentin Chary9e1565b2011-02-06 13:28:36 +01001039static mode_t eeepc_sysfs_is_visible(struct kobject *kobj,
1040 struct attribute *attr,
1041 int idx)
1042{
1043 bool supported = true;
1044 int devid = -1;
1045
1046 if (attr == &dev_attr_camera.attr)
1047 devid = EEEPC_WMI_DEVID_CAMERA;
1048 else if (attr == &dev_attr_cardr.attr)
1049 devid = EEEPC_WMI_DEVID_CARDREADER;
1050
1051 if (devid != -1)
1052 supported = eeepc_wmi_get_devstate_simple(devid) != -ENODEV;
1053
1054 return supported ? attr->mode : 0;
1055}
1056
Corentin Chary4e37b422010-11-29 08:14:08 +01001057static struct attribute_group platform_attribute_group = {
Corentin Chary9e1565b2011-02-06 13:28:36 +01001058 .is_visible = eeepc_sysfs_is_visible,
1059 .attrs = platform_attributes
Corentin Chary4e37b422010-11-29 08:14:08 +01001060};
1061
Chris Bagwell7f80d732010-10-11 18:47:18 -05001062static void eeepc_wmi_sysfs_exit(struct platform_device *device)
1063{
Corentin Chary4e37b422010-11-29 08:14:08 +01001064 sysfs_remove_group(&device->dev.kobj, &platform_attribute_group);
Chris Bagwell7f80d732010-10-11 18:47:18 -05001065}
1066
1067static int eeepc_wmi_sysfs_init(struct platform_device *device)
1068{
Corentin Chary4e37b422010-11-29 08:14:08 +01001069 return sysfs_create_group(&device->dev.kobj, &platform_attribute_group);
Chris Bagwell7f80d732010-10-11 18:47:18 -05001070}
1071
Corentin Chary27c136c2010-11-29 08:14:05 +01001072/*
1073 * Platform device
1074 */
1075static int __init eeepc_wmi_platform_init(struct eeepc_wmi *eeepc)
1076{
Corentin Charya04ce292011-02-06 13:28:33 +01001077 return eeepc_wmi_sysfs_init(eeepc->platform_device);
Corentin Chary27c136c2010-11-29 08:14:05 +01001078}
1079
1080static void eeepc_wmi_platform_exit(struct eeepc_wmi *eeepc)
1081{
1082 eeepc_wmi_sysfs_exit(eeepc->platform_device);
Corentin Chary27c136c2010-11-29 08:14:05 +01001083}
1084
1085/*
Corentin Chary8c1b2d82010-11-29 08:14:09 +01001086 * debugfs
1087 */
1088struct eeepc_wmi_debugfs_node {
1089 struct eeepc_wmi *eeepc;
1090 char *name;
1091 int (*show)(struct seq_file *m, void *data);
1092};
1093
1094static int show_dsts(struct seq_file *m, void *data)
1095{
1096 struct eeepc_wmi *eeepc = m->private;
1097 acpi_status status;
1098 u32 retval = -1;
1099
1100 status = eeepc_wmi_get_devstate(eeepc->debug.dev_id, &retval);
1101
1102 if (ACPI_FAILURE(status))
1103 return -EIO;
1104
1105 seq_printf(m, "DSTS(%x) = %x\n", eeepc->debug.dev_id, retval);
1106
1107 return 0;
1108}
1109
1110static int show_devs(struct seq_file *m, void *data)
1111{
1112 struct eeepc_wmi *eeepc = m->private;
1113 acpi_status status;
1114 u32 retval = -1;
1115
1116 status = eeepc_wmi_set_devstate(eeepc->debug.dev_id,
1117 eeepc->debug.ctrl_param, &retval);
1118 if (ACPI_FAILURE(status))
1119 return -EIO;
1120
1121 seq_printf(m, "DEVS(%x, %x) = %x\n", eeepc->debug.dev_id,
1122 eeepc->debug.ctrl_param, retval);
1123
1124 return 0;
1125}
1126
1127static struct eeepc_wmi_debugfs_node eeepc_wmi_debug_files[] = {
1128 { NULL, "devs", show_devs },
1129 { NULL, "dsts", show_dsts },
1130};
1131
1132static int eeepc_wmi_debugfs_open(struct inode *inode, struct file *file)
1133{
1134 struct eeepc_wmi_debugfs_node *node = inode->i_private;
1135
1136 return single_open(file, node->show, node->eeepc);
1137}
1138
1139static const struct file_operations eeepc_wmi_debugfs_io_ops = {
1140 .owner = THIS_MODULE,
1141 .open = eeepc_wmi_debugfs_open,
1142 .read = seq_read,
1143 .llseek = seq_lseek,
1144 .release = single_release,
1145};
1146
1147static void eeepc_wmi_debugfs_exit(struct eeepc_wmi *eeepc)
1148{
1149 debugfs_remove_recursive(eeepc->debug.root);
1150}
1151
1152static int eeepc_wmi_debugfs_init(struct eeepc_wmi *eeepc)
1153{
1154 struct dentry *dent;
1155 int i;
1156
1157 eeepc->debug.root = debugfs_create_dir(EEEPC_WMI_FILE, NULL);
1158 if (!eeepc->debug.root) {
1159 pr_err("failed to create debugfs directory");
1160 goto error_debugfs;
1161 }
1162
1163 dent = debugfs_create_x32("dev_id", S_IRUGO|S_IWUSR,
1164 eeepc->debug.root, &eeepc->debug.dev_id);
1165 if (!dent)
1166 goto error_debugfs;
1167
1168 dent = debugfs_create_x32("ctrl_param", S_IRUGO|S_IWUSR,
1169 eeepc->debug.root, &eeepc->debug.ctrl_param);
1170 if (!dent)
1171 goto error_debugfs;
1172
1173 for (i = 0; i < ARRAY_SIZE(eeepc_wmi_debug_files); i++) {
1174 struct eeepc_wmi_debugfs_node *node = &eeepc_wmi_debug_files[i];
1175
1176 node->eeepc = eeepc;
1177 dent = debugfs_create_file(node->name, S_IFREG | S_IRUGO,
1178 eeepc->debug.root, node,
1179 &eeepc_wmi_debugfs_io_ops);
1180 if (!dent) {
1181 pr_err("failed to create debug file: %s\n", node->name);
1182 goto error_debugfs;
1183 }
1184 }
1185
1186 return 0;
1187
1188error_debugfs:
1189 eeepc_wmi_debugfs_exit(eeepc);
1190 return -ENOMEM;
1191}
1192
1193/*
Corentin Chary27c136c2010-11-29 08:14:05 +01001194 * WMI Driver
1195 */
Corentin Charyafa7c882011-02-06 13:28:28 +01001196static void eeepc_dmi_check(struct eeepc_wmi *eeepc)
1197{
1198 const char *model;
1199
1200 model = dmi_get_system_info(DMI_PRODUCT_NAME);
1201 if (!model)
1202 return;
1203
1204 /*
1205 * Whitelist for wlan hotplug
1206 *
1207 * Eeepc 1000H needs the current hotplug code to handle
1208 * Fn+F2 correctly. We may add other Eeepc here later, but
1209 * it seems that most of the laptops supported by eeepc-wmi
1210 * don't need to be on this list
1211 */
1212 if (strcmp(model, "1000H") == 0) {
1213 eeepc->hotplug_wireless = true;
1214 pr_info("wlan hotplug enabled\n");
1215 }
1216}
1217
Corentin Charya04ce292011-02-06 13:28:33 +01001218static int __init eeepc_wmi_add(struct platform_device *pdev)
Yong Wangee027e42010-03-21 10:26:34 +08001219{
Yong Wang45f2c692010-04-11 09:27:19 +08001220 struct eeepc_wmi *eeepc;
Yong Wangee027e42010-03-21 10:26:34 +08001221 acpi_status status;
Corentin Chary27c136c2010-11-29 08:14:05 +01001222 int err;
Yong Wangee027e42010-03-21 10:26:34 +08001223
Corentin Chary27c136c2010-11-29 08:14:05 +01001224 eeepc = kzalloc(sizeof(struct eeepc_wmi), GFP_KERNEL);
1225 if (!eeepc)
Corentin Charya04ce292011-02-06 13:28:33 +01001226 return -ENOMEM;
1227
1228 eeepc->platform_device = pdev;
1229 platform_set_drvdata(eeepc->platform_device, eeepc);
Corentin Chary27c136c2010-11-29 08:14:05 +01001230
Corentin Charyafa7c882011-02-06 13:28:28 +01001231 eeepc->hotplug_wireless = hotplug_wireless;
1232 eeepc_dmi_check(eeepc);
1233
Corentin Chary27c136c2010-11-29 08:14:05 +01001234 err = eeepc_wmi_platform_init(eeepc);
1235 if (err)
1236 goto fail_platform;
Yong Wang45f2c692010-04-11 09:27:19 +08001237
1238 err = eeepc_wmi_input_init(eeepc);
1239 if (err)
Corentin Chary27c136c2010-11-29 08:14:05 +01001240 goto fail_input;
Yong Wang3d7b1652010-04-11 09:27:54 +08001241
Corentin Chary084fca62010-11-29 08:14:06 +01001242 err = eeepc_wmi_led_init(eeepc);
1243 if (err)
1244 goto fail_leds;
1245
Corentin Charyba48fdb2010-11-29 08:14:07 +01001246 err = eeepc_wmi_rfkill_init(eeepc);
1247 if (err)
1248 goto fail_rfkill;
1249
Yong Wang3d7b1652010-04-11 09:27:54 +08001250 if (!acpi_video_backlight_support()) {
1251 err = eeepc_wmi_backlight_init(eeepc);
Corentin Charyb7187262011-02-06 13:28:39 +01001252 if (err && err != -ENODEV)
Corentin Chary27c136c2010-11-29 08:14:05 +01001253 goto fail_backlight;
Yong Wang3d7b1652010-04-11 09:27:54 +08001254 } else
1255 pr_info("Backlight controlled by ACPI video driver\n");
Yong Wang45f2c692010-04-11 09:27:19 +08001256
1257 status = wmi_install_notify_handler(EEEPC_WMI_EVENT_GUID,
Corentin Chary27c136c2010-11-29 08:14:05 +01001258 eeepc_wmi_notify, eeepc);
Yong Wang45f2c692010-04-11 09:27:19 +08001259 if (ACPI_FAILURE(status)) {
1260 pr_err("Unable to register notify handler - %d\n",
1261 status);
1262 err = -ENODEV;
Corentin Chary27c136c2010-11-29 08:14:05 +01001263 goto fail_wmi_handler;
Yong Wang45f2c692010-04-11 09:27:19 +08001264 }
1265
Corentin Chary8c1b2d82010-11-29 08:14:09 +01001266 err = eeepc_wmi_debugfs_init(eeepc);
1267 if (err)
1268 goto fail_debugfs;
1269
Corentin Charya04ce292011-02-06 13:28:33 +01001270 return 0;
Yong Wang45f2c692010-04-11 09:27:19 +08001271
Corentin Chary8c1b2d82010-11-29 08:14:09 +01001272fail_debugfs:
1273 wmi_remove_notify_handler(EEEPC_WMI_EVENT_GUID);
Corentin Chary27c136c2010-11-29 08:14:05 +01001274fail_wmi_handler:
Yong Wang3d7b1652010-04-11 09:27:54 +08001275 eeepc_wmi_backlight_exit(eeepc);
Corentin Chary27c136c2010-11-29 08:14:05 +01001276fail_backlight:
Corentin Charyba48fdb2010-11-29 08:14:07 +01001277 eeepc_wmi_rfkill_exit(eeepc);
1278fail_rfkill:
Corentin Chary084fca62010-11-29 08:14:06 +01001279 eeepc_wmi_led_exit(eeepc);
1280fail_leds:
Yong Wang45f2c692010-04-11 09:27:19 +08001281 eeepc_wmi_input_exit(eeepc);
Corentin Chary27c136c2010-11-29 08:14:05 +01001282fail_input:
1283 eeepc_wmi_platform_exit(eeepc);
1284fail_platform:
1285 kfree(eeepc);
Corentin Charya04ce292011-02-06 13:28:33 +01001286 return err;
Yong Wang45f2c692010-04-11 09:27:19 +08001287}
1288
Corentin Charya04ce292011-02-06 13:28:33 +01001289static int __exit eeepc_wmi_remove(struct platform_device *device)
Yong Wang45f2c692010-04-11 09:27:19 +08001290{
1291 struct eeepc_wmi *eeepc;
1292
1293 eeepc = platform_get_drvdata(device);
1294 wmi_remove_notify_handler(EEEPC_WMI_EVENT_GUID);
Yong Wang3d7b1652010-04-11 09:27:54 +08001295 eeepc_wmi_backlight_exit(eeepc);
Yong Wang45f2c692010-04-11 09:27:19 +08001296 eeepc_wmi_input_exit(eeepc);
Corentin Chary084fca62010-11-29 08:14:06 +01001297 eeepc_wmi_led_exit(eeepc);
Corentin Charyba48fdb2010-11-29 08:14:07 +01001298 eeepc_wmi_rfkill_exit(eeepc);
Corentin Chary8c1b2d82010-11-29 08:14:09 +01001299 eeepc_wmi_debugfs_exit(eeepc);
Corentin Chary27c136c2010-11-29 08:14:05 +01001300 eeepc_wmi_platform_exit(eeepc);
Yong Wang45f2c692010-04-11 09:27:19 +08001301
Corentin Chary27c136c2010-11-29 08:14:05 +01001302 kfree(eeepc);
Yong Wang45f2c692010-04-11 09:27:19 +08001303 return 0;
1304}
1305
Corentin Chary0773d7f2011-02-06 13:28:32 +01001306/*
1307 * Platform driver - hibernate/resume callbacks
1308 */
1309static int eeepc_hotk_thaw(struct device *device)
1310{
1311 struct eeepc_wmi *eeepc = dev_get_drvdata(device);
1312
1313 if (eeepc->wlan_rfkill) {
1314 bool wlan;
1315
1316 /*
1317 * Work around bios bug - acpi _PTS turns off the wireless led
1318 * during suspend. Normally it restores it on resume, but
1319 * we should kick it ourselves in case hibernation is aborted.
1320 */
1321 wlan = eeepc_wmi_get_devstate_simple(EEEPC_WMI_DEVID_WLAN);
1322 eeepc_wmi_set_devstate(EEEPC_WMI_DEVID_WLAN, wlan, NULL);
1323 }
1324
1325 return 0;
1326}
1327
1328static int eeepc_hotk_restore(struct device *device)
1329{
1330 struct eeepc_wmi *eeepc = dev_get_drvdata(device);
1331 int bl;
1332
1333 /* Refresh both wlan rfkill state and pci hotplug */
1334 if (eeepc->wlan_rfkill)
1335 eeepc_rfkill_hotplug(eeepc);
1336
1337 if (eeepc->bluetooth_rfkill) {
1338 bl = !eeepc_wmi_get_devstate_simple(EEEPC_WMI_DEVID_BLUETOOTH);
1339 rfkill_set_sw_state(eeepc->bluetooth_rfkill, bl);
Corentin Chary2e9e1592011-02-06 13:28:37 +01001340 }
1341 if (eeepc->wimax_rfkill) {
1342 bl = !eeepc_wmi_get_devstate_simple(EEEPC_WMI_DEVID_WIMAX);
1343 rfkill_set_sw_state(eeepc->wimax_rfkill, bl);
1344 }
Corentin Chary0773d7f2011-02-06 13:28:32 +01001345 if (eeepc->wwan3g_rfkill) {
1346 bl = !eeepc_wmi_get_devstate_simple(EEEPC_WMI_DEVID_WWAN3G);
1347 rfkill_set_sw_state(eeepc->wwan3g_rfkill, bl);
1348 }
1349
1350 return 0;
1351}
1352
1353static const struct dev_pm_ops eeepc_pm_ops = {
1354 .thaw = eeepc_hotk_thaw,
1355 .restore = eeepc_hotk_restore,
1356};
1357
Yong Wang45f2c692010-04-11 09:27:19 +08001358static struct platform_driver platform_driver = {
Corentin Charya04ce292011-02-06 13:28:33 +01001359 .remove = __exit_p(eeepc_wmi_remove),
Yong Wang45f2c692010-04-11 09:27:19 +08001360 .driver = {
1361 .name = EEEPC_WMI_FILE,
1362 .owner = THIS_MODULE,
Corentin Chary0773d7f2011-02-06 13:28:32 +01001363 .pm = &eeepc_pm_ops,
Yong Wang45f2c692010-04-11 09:27:19 +08001364 },
Yong Wang45f2c692010-04-11 09:27:19 +08001365};
1366
Corentin Charyd358cb52010-11-29 08:14:14 +01001367static acpi_status __init eeepc_wmi_parse_device(acpi_handle handle, u32 level,
1368 void *context, void **retval)
1369{
1370 pr_warning("Found legacy ATKD device (%s)", EEEPC_ACPI_HID);
1371 *(bool *)context = true;
1372 return AE_CTRL_TERMINATE;
1373}
1374
1375static int __init eeepc_wmi_check_atkd(void)
1376{
1377 acpi_status status;
1378 bool found = false;
1379
1380 status = acpi_get_devices(EEEPC_ACPI_HID, eeepc_wmi_parse_device,
1381 &found, NULL);
1382
1383 if (ACPI_FAILURE(status) || !found)
1384 return 0;
1385 return -1;
1386}
1387
Corentin Charya04ce292011-02-06 13:28:33 +01001388static int __init eeepc_wmi_probe(struct platform_device *pdev)
Yong Wang45f2c692010-04-11 09:27:19 +08001389{
Yong Wang3d7b1652010-04-11 09:27:54 +08001390 if (!wmi_has_guid(EEEPC_WMI_EVENT_GUID) ||
1391 !wmi_has_guid(EEEPC_WMI_MGMT_GUID)) {
Yong Wang81248882010-04-11 09:26:33 +08001392 pr_warning("No known WMI GUID found\n");
Yong Wangee027e42010-03-21 10:26:34 +08001393 return -ENODEV;
1394 }
1395
Corentin Charyd358cb52010-11-29 08:14:14 +01001396 if (eeepc_wmi_check_atkd()) {
1397 pr_warning("WMI device present, but legacy ATKD device is also "
1398 "present and enabled.");
1399 pr_warning("You probably booted with acpi_osi=\"Linux\" or "
1400 "acpi_osi=\"!Windows 2009\"");
1401 pr_warning("Can't load eeepc-wmi, use default acpi_osi "
1402 "(preferred) or eeepc-laptop");
1403 return -ENODEV;
1404 }
1405
Corentin Charya04ce292011-02-06 13:28:33 +01001406 return eeepc_wmi_add(pdev);
1407}
Yong Wangee027e42010-03-21 10:26:34 +08001408
Corentin Charya04ce292011-02-06 13:28:33 +01001409static struct platform_device *platform_device;
Yong Wangee027e42010-03-21 10:26:34 +08001410
Corentin Charya04ce292011-02-06 13:28:33 +01001411static int __init eeepc_wmi_init(void)
1412{
1413 platform_device = platform_create_bundle(&platform_driver,
1414 eeepc_wmi_probe,
1415 NULL, 0, NULL, 0);
1416 if (IS_ERR(platform_device))
1417 return PTR_ERR(platform_device);
Yong Wangee027e42010-03-21 10:26:34 +08001418 return 0;
1419}
1420
1421static void __exit eeepc_wmi_exit(void)
1422{
Corentin Charya04ce292011-02-06 13:28:33 +01001423 platform_device_unregister(platform_device);
Yong Wang45f2c692010-04-11 09:27:19 +08001424 platform_driver_unregister(&platform_driver);
Yong Wangee027e42010-03-21 10:26:34 +08001425}
1426
1427module_init(eeepc_wmi_init);
1428module_exit(eeepc_wmi_exit);