blob: 350b7bcfc3260b4580e4ee04c1c5b0ef147f07aa [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 Chary8c1b2d82010-11-29 08:14:09 +010040#include <linux/debugfs.h>
41#include <linux/seq_file.h>
Yong Wang45f2c692010-04-11 09:27:19 +080042#include <linux/platform_device.h>
Yong Wangee027e42010-03-21 10:26:34 +080043#include <acpi/acpi_bus.h>
44#include <acpi/acpi_drivers.h>
45
Yong Wang45f2c692010-04-11 09:27:19 +080046#define EEEPC_WMI_FILE "eeepc-wmi"
47
Yong Wangee027e42010-03-21 10:26:34 +080048MODULE_AUTHOR("Yong Wang <yong.y.wang@intel.com>");
49MODULE_DESCRIPTION("Eee PC WMI Hotkey Driver");
50MODULE_LICENSE("GPL");
51
Corentin Charyd358cb52010-11-29 08:14:14 +010052#define EEEPC_ACPI_HID "ASUS010" /* old _HID used in eeepc-laptop */
53
Yong Wangee027e42010-03-21 10:26:34 +080054#define EEEPC_WMI_EVENT_GUID "ABBC0F72-8EA1-11D1-00A0-C90629100000"
Yong Wang3d7b1652010-04-11 09:27:54 +080055#define EEEPC_WMI_MGMT_GUID "97845ED0-4E6D-11DE-8A39-0800200C9A66"
Yong Wangee027e42010-03-21 10:26:34 +080056
57MODULE_ALIAS("wmi:"EEEPC_WMI_EVENT_GUID);
Yong Wang3d7b1652010-04-11 09:27:54 +080058MODULE_ALIAS("wmi:"EEEPC_WMI_MGMT_GUID);
Yong Wangee027e42010-03-21 10:26:34 +080059
60#define NOTIFY_BRNUP_MIN 0x11
61#define NOTIFY_BRNUP_MAX 0x1f
62#define NOTIFY_BRNDOWN_MIN 0x20
63#define NOTIFY_BRNDOWN_MAX 0x2e
64
Yong Wang3d7b1652010-04-11 09:27:54 +080065#define EEEPC_WMI_METHODID_DEVS 0x53564544
66#define EEEPC_WMI_METHODID_DSTS 0x53544344
Chris Bagwell7f80d732010-10-11 18:47:18 -050067#define EEEPC_WMI_METHODID_CFVS 0x53564643
Yong Wang3d7b1652010-04-11 09:27:54 +080068
69#define EEEPC_WMI_DEVID_BACKLIGHT 0x00050012
Corentin Chary084fca62010-11-29 08:14:06 +010070#define EEEPC_WMI_DEVID_TPDLED 0x00100011
Corentin Charyba48fdb2010-11-29 08:14:07 +010071#define EEEPC_WMI_DEVID_WLAN 0x00010011
72#define EEEPC_WMI_DEVID_BLUETOOTH 0x00010013
73#define EEEPC_WMI_DEVID_WWAN3G 0x00010019
Yong Wang3d7b1652010-04-11 09:27:54 +080074
Yong Wangee027e42010-03-21 10:26:34 +080075static const struct key_entry eeepc_wmi_keymap[] = {
76 /* Sleep already handled via generic ACPI code */
Yong Wangee027e42010-03-21 10:26:34 +080077 { KE_IGNORE, NOTIFY_BRNDOWN_MIN, { KEY_BRIGHTNESSDOWN } },
78 { KE_IGNORE, NOTIFY_BRNUP_MIN, { KEY_BRIGHTNESSUP } },
Corentin Chary5628e5a2011-02-06 13:28:26 +010079 { KE_KEY, 0x30, { KEY_VOLUMEUP } },
80 { KE_KEY, 0x31, { KEY_VOLUMEDOWN } },
81 { KE_KEY, 0x32, { KEY_MUTE } },
82 { KE_KEY, 0x5c, { KEY_F15 } },
83 { KE_KEY, 0x5d, { KEY_WLAN } },
Chris Bagwelleda17482010-10-11 18:47:17 -050084 { KE_KEY, 0x6b, { KEY_F13 } }, /* Disable Touchpad */
Corentin Chary5628e5a2011-02-06 13:28:26 +010085 { KE_KEY, 0xcc, { KEY_SWITCHVIDEOMODE } },
86 { KE_KEY, 0xe0, { KEY_PROG1 } },
Chris Bagwelleda17482010-10-11 18:47:17 -050087 { KE_KEY, 0xe1, { KEY_F14 } },
88 { KE_KEY, 0xe9, { KEY_DISPLAY_OFF } },
Yong Wangee027e42010-03-21 10:26:34 +080089 { KE_END, 0},
90};
91
Yong Wang3d7b1652010-04-11 09:27:54 +080092struct bios_args {
93 u32 dev_id;
94 u32 ctrl_param;
95};
96
Corentin Chary8c1b2d82010-11-29 08:14:09 +010097/*
98 * eeepc-wmi/ - debugfs root directory
99 * dev_id - current dev_id
100 * ctrl_param - current ctrl_param
101 * devs - call DEVS(dev_id, ctrl_param) and print result
102 * dsts - call DSTS(dev_id) and print result
103 */
104struct eeepc_wmi_debug {
105 struct dentry *root;
106 u32 dev_id;
107 u32 ctrl_param;
108};
109
Yong Wang81248882010-04-11 09:26:33 +0800110struct eeepc_wmi {
111 struct input_dev *inputdev;
Yong Wang3d7b1652010-04-11 09:27:54 +0800112 struct backlight_device *backlight_device;
Corentin Chary27c136c2010-11-29 08:14:05 +0100113 struct platform_device *platform_device;
Corentin Chary084fca62010-11-29 08:14:06 +0100114
115 struct led_classdev tpd_led;
116 int tpd_led_wk;
117 struct workqueue_struct *led_workqueue;
118 struct work_struct tpd_led_work;
Corentin Charyba48fdb2010-11-29 08:14:07 +0100119
120 struct rfkill *wlan_rfkill;
121 struct rfkill *bluetooth_rfkill;
122 struct rfkill *wwan3g_rfkill;
Corentin Chary8c1b2d82010-11-29 08:14:09 +0100123
124 struct eeepc_wmi_debug debug;
Yong Wang81248882010-04-11 09:26:33 +0800125};
126
Corentin Chary27c136c2010-11-29 08:14:05 +0100127/* Only used in eeepc_wmi_init() and eeepc_wmi_exit() */
Yong Wang45f2c692010-04-11 09:27:19 +0800128static struct platform_device *platform_device;
Yong Wangee027e42010-03-21 10:26:34 +0800129
Yong Wang81248882010-04-11 09:26:33 +0800130static int eeepc_wmi_input_init(struct eeepc_wmi *eeepc)
Yong Wangee027e42010-03-21 10:26:34 +0800131{
132 int err;
133
Yong Wang81248882010-04-11 09:26:33 +0800134 eeepc->inputdev = input_allocate_device();
135 if (!eeepc->inputdev)
Yong Wangee027e42010-03-21 10:26:34 +0800136 return -ENOMEM;
137
Yong Wang81248882010-04-11 09:26:33 +0800138 eeepc->inputdev->name = "Eee PC WMI hotkeys";
Yong Wang45f2c692010-04-11 09:27:19 +0800139 eeepc->inputdev->phys = EEEPC_WMI_FILE "/input0";
Yong Wang81248882010-04-11 09:26:33 +0800140 eeepc->inputdev->id.bustype = BUS_HOST;
Corentin Chary27c136c2010-11-29 08:14:05 +0100141 eeepc->inputdev->dev.parent = &eeepc->platform_device->dev;
Yong Wangee027e42010-03-21 10:26:34 +0800142
Yong Wang81248882010-04-11 09:26:33 +0800143 err = sparse_keymap_setup(eeepc->inputdev, eeepc_wmi_keymap, NULL);
Yong Wangee027e42010-03-21 10:26:34 +0800144 if (err)
145 goto err_free_dev;
146
Yong Wang81248882010-04-11 09:26:33 +0800147 err = input_register_device(eeepc->inputdev);
Yong Wangee027e42010-03-21 10:26:34 +0800148 if (err)
149 goto err_free_keymap;
150
151 return 0;
152
153err_free_keymap:
Yong Wang81248882010-04-11 09:26:33 +0800154 sparse_keymap_free(eeepc->inputdev);
Yong Wangee027e42010-03-21 10:26:34 +0800155err_free_dev:
Yong Wang81248882010-04-11 09:26:33 +0800156 input_free_device(eeepc->inputdev);
Yong Wangee027e42010-03-21 10:26:34 +0800157 return err;
158}
159
Yong Wang81248882010-04-11 09:26:33 +0800160static void eeepc_wmi_input_exit(struct eeepc_wmi *eeepc)
161{
162 if (eeepc->inputdev) {
163 sparse_keymap_free(eeepc->inputdev);
164 input_unregister_device(eeepc->inputdev);
165 }
166
167 eeepc->inputdev = NULL;
168}
169
Corentin Chary2a3f0062010-11-29 08:14:10 +0100170static acpi_status eeepc_wmi_get_devstate(u32 dev_id, u32 *retval)
Yong Wang3d7b1652010-04-11 09:27:54 +0800171{
172 struct acpi_buffer input = { (acpi_size)sizeof(u32), &dev_id };
173 struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER, NULL };
174 union acpi_object *obj;
175 acpi_status status;
176 u32 tmp;
177
178 status = wmi_evaluate_method(EEEPC_WMI_MGMT_GUID,
179 1, EEEPC_WMI_METHODID_DSTS, &input, &output);
180
181 if (ACPI_FAILURE(status))
182 return status;
183
184 obj = (union acpi_object *)output.pointer;
185 if (obj && obj->type == ACPI_TYPE_INTEGER)
186 tmp = (u32)obj->integer.value;
187 else
188 tmp = 0;
189
Corentin Chary2a3f0062010-11-29 08:14:10 +0100190 if (retval)
191 *retval = tmp;
Yong Wang3d7b1652010-04-11 09:27:54 +0800192
193 kfree(obj);
194
195 return status;
196
197}
198
Corentin Chary8c1b2d82010-11-29 08:14:09 +0100199static acpi_status eeepc_wmi_set_devstate(u32 dev_id, u32 ctrl_param,
200 u32 *retval)
Yong Wang3d7b1652010-04-11 09:27:54 +0800201{
202 struct bios_args args = {
203 .dev_id = dev_id,
204 .ctrl_param = ctrl_param,
205 };
206 struct acpi_buffer input = { (acpi_size)sizeof(args), &args };
207 acpi_status status;
208
Corentin Chary8c1b2d82010-11-29 08:14:09 +0100209 if (!retval) {
210 status = wmi_evaluate_method(EEEPC_WMI_MGMT_GUID, 1,
211 EEEPC_WMI_METHODID_DEVS,
212 &input, NULL);
213 } else {
214 struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER, NULL };
215 union acpi_object *obj;
216 u32 tmp;
217
218 status = wmi_evaluate_method(EEEPC_WMI_MGMT_GUID, 1,
219 EEEPC_WMI_METHODID_DEVS,
220 &input, &output);
221
222 if (ACPI_FAILURE(status))
223 return status;
224
225 obj = (union acpi_object *)output.pointer;
226 if (obj && obj->type == ACPI_TYPE_INTEGER)
227 tmp = (u32)obj->integer.value;
228 else
229 tmp = 0;
230
231 *retval = tmp;
232
233 kfree(obj);
234 }
Yong Wang3d7b1652010-04-11 09:27:54 +0800235
236 return status;
237}
238
Corentin Chary084fca62010-11-29 08:14:06 +0100239/*
240 * LEDs
241 */
242/*
243 * These functions actually update the LED's, and are called from a
244 * workqueue. By doing this as separate work rather than when the LED
245 * subsystem asks, we avoid messing with the Eeepc ACPI stuff during a
246 * potentially bad time, such as a timer interrupt.
247 */
248static void tpd_led_update(struct work_struct *work)
249{
250 int ctrl_param;
251 struct eeepc_wmi *eeepc;
252
253 eeepc = container_of(work, struct eeepc_wmi, tpd_led_work);
254
255 ctrl_param = eeepc->tpd_led_wk;
Corentin Chary8c1b2d82010-11-29 08:14:09 +0100256 eeepc_wmi_set_devstate(EEEPC_WMI_DEVID_TPDLED, ctrl_param, NULL);
Corentin Chary084fca62010-11-29 08:14:06 +0100257}
258
259static void tpd_led_set(struct led_classdev *led_cdev,
260 enum led_brightness value)
261{
262 struct eeepc_wmi *eeepc;
263
264 eeepc = container_of(led_cdev, struct eeepc_wmi, tpd_led);
265
266 eeepc->tpd_led_wk = !!value;
267 queue_work(eeepc->led_workqueue, &eeepc->tpd_led_work);
268}
269
270static int read_tpd_state(struct eeepc_wmi *eeepc)
271{
Corentin Charydfed65d2010-11-29 08:14:12 +0100272 u32 retval;
Corentin Chary084fca62010-11-29 08:14:06 +0100273 acpi_status status;
274
Corentin Chary2a3f0062010-11-29 08:14:10 +0100275 status = eeepc_wmi_get_devstate(EEEPC_WMI_DEVID_TPDLED, &retval);
Corentin Chary084fca62010-11-29 08:14:06 +0100276
277 if (ACPI_FAILURE(status))
278 return -1;
Corentin Chary2a3f0062010-11-29 08:14:10 +0100279 else if (!retval || retval == 0x00060000)
Corentin Chary084fca62010-11-29 08:14:06 +0100280 /*
281 * if touchpad led is present, DSTS will set some bits,
282 * usually 0x00020000.
283 * 0x00060000 means that the device is not supported
284 */
285 return -ENODEV;
286 else
287 /* Status is stored in the first bit */
Corentin Chary2a3f0062010-11-29 08:14:10 +0100288 return retval & 0x1;
Corentin Chary084fca62010-11-29 08:14:06 +0100289}
290
291static enum led_brightness tpd_led_get(struct led_classdev *led_cdev)
292{
293 struct eeepc_wmi *eeepc;
294
295 eeepc = container_of(led_cdev, struct eeepc_wmi, tpd_led);
296
297 return read_tpd_state(eeepc);
298}
299
300static int eeepc_wmi_led_init(struct eeepc_wmi *eeepc)
301{
302 int rv;
303
304 if (read_tpd_state(eeepc) < 0)
305 return 0;
306
307 eeepc->led_workqueue = create_singlethread_workqueue("led_workqueue");
308 if (!eeepc->led_workqueue)
309 return -ENOMEM;
310 INIT_WORK(&eeepc->tpd_led_work, tpd_led_update);
311
312 eeepc->tpd_led.name = "eeepc::touchpad";
313 eeepc->tpd_led.brightness_set = tpd_led_set;
314 eeepc->tpd_led.brightness_get = tpd_led_get;
315 eeepc->tpd_led.max_brightness = 1;
316
317 rv = led_classdev_register(&eeepc->platform_device->dev,
318 &eeepc->tpd_led);
319 if (rv) {
320 destroy_workqueue(eeepc->led_workqueue);
321 return rv;
322 }
323
324 return 0;
325}
326
327static void eeepc_wmi_led_exit(struct eeepc_wmi *eeepc)
328{
329 if (eeepc->tpd_led.dev)
330 led_classdev_unregister(&eeepc->tpd_led);
331 if (eeepc->led_workqueue)
332 destroy_workqueue(eeepc->led_workqueue);
333}
334
335/*
Corentin Charyba48fdb2010-11-29 08:14:07 +0100336 * Rfkill devices
337 */
338static int eeepc_rfkill_set(void *data, bool blocked)
339{
340 int dev_id = (unsigned long)data;
341 u32 ctrl_param = !blocked;
342
Corentin Chary8c1b2d82010-11-29 08:14:09 +0100343 return eeepc_wmi_set_devstate(dev_id, ctrl_param, NULL);
Corentin Charyba48fdb2010-11-29 08:14:07 +0100344}
345
346static void eeepc_rfkill_query(struct rfkill *rfkill, void *data)
347{
348 int dev_id = (unsigned long)data;
Corentin Chary2a3f0062010-11-29 08:14:10 +0100349 u32 retval;
Corentin Charyba48fdb2010-11-29 08:14:07 +0100350 acpi_status status;
351
Corentin Chary2a3f0062010-11-29 08:14:10 +0100352 status = eeepc_wmi_get_devstate(dev_id, &retval);
Corentin Charyba48fdb2010-11-29 08:14:07 +0100353
354 if (ACPI_FAILURE(status))
355 return ;
356
Corentin Chary2a3f0062010-11-29 08:14:10 +0100357 rfkill_set_sw_state(rfkill, !(retval & 0x1));
Corentin Charyba48fdb2010-11-29 08:14:07 +0100358}
359
360static const struct rfkill_ops eeepc_rfkill_ops = {
361 .set_block = eeepc_rfkill_set,
362 .query = eeepc_rfkill_query,
363};
364
365static int eeepc_new_rfkill(struct eeepc_wmi *eeepc,
366 struct rfkill **rfkill,
367 const char *name,
368 enum rfkill_type type, int dev_id)
369{
370 int result;
Corentin Chary2a3f0062010-11-29 08:14:10 +0100371 u32 retval;
Corentin Charyba48fdb2010-11-29 08:14:07 +0100372 acpi_status status;
373
Corentin Chary2a3f0062010-11-29 08:14:10 +0100374 status = eeepc_wmi_get_devstate(dev_id, &retval);
Corentin Charyba48fdb2010-11-29 08:14:07 +0100375
376 if (ACPI_FAILURE(status))
377 return -1;
378
379 /* If the device is present, DSTS will always set some bits
380 * 0x00070000 - 1110000000000000000 - device supported
381 * 0x00060000 - 1100000000000000000 - not supported
382 * 0x00020000 - 0100000000000000000 - device supported
383 * 0x00010000 - 0010000000000000000 - not supported / special mode ?
384 */
Corentin Chary2a3f0062010-11-29 08:14:10 +0100385 if (!retval || retval == 0x00060000)
Corentin Charyba48fdb2010-11-29 08:14:07 +0100386 return -ENODEV;
387
388 *rfkill = rfkill_alloc(name, &eeepc->platform_device->dev, type,
389 &eeepc_rfkill_ops, (void *)(long)dev_id);
390
391 if (!*rfkill)
392 return -EINVAL;
393
Corentin Chary2a3f0062010-11-29 08:14:10 +0100394 rfkill_init_sw_state(*rfkill, !(retval & 0x1));
Corentin Charyba48fdb2010-11-29 08:14:07 +0100395 result = rfkill_register(*rfkill);
396 if (result) {
397 rfkill_destroy(*rfkill);
398 *rfkill = NULL;
399 return result;
400 }
401 return 0;
402}
403
404static void eeepc_wmi_rfkill_exit(struct eeepc_wmi *eeepc)
405{
406 if (eeepc->wlan_rfkill) {
407 rfkill_unregister(eeepc->wlan_rfkill);
408 rfkill_destroy(eeepc->wlan_rfkill);
409 eeepc->wlan_rfkill = NULL;
410 }
411 if (eeepc->bluetooth_rfkill) {
412 rfkill_unregister(eeepc->bluetooth_rfkill);
413 rfkill_destroy(eeepc->bluetooth_rfkill);
414 eeepc->bluetooth_rfkill = NULL;
415 }
416 if (eeepc->wwan3g_rfkill) {
417 rfkill_unregister(eeepc->wwan3g_rfkill);
418 rfkill_destroy(eeepc->wwan3g_rfkill);
419 eeepc->wwan3g_rfkill = NULL;
420 }
421}
422
423static int eeepc_wmi_rfkill_init(struct eeepc_wmi *eeepc)
424{
425 int result = 0;
426
427 result = eeepc_new_rfkill(eeepc, &eeepc->wlan_rfkill,
428 "eeepc-wlan", RFKILL_TYPE_WLAN,
429 EEEPC_WMI_DEVID_WLAN);
430
431 if (result && result != -ENODEV)
432 goto exit;
433
434 result = eeepc_new_rfkill(eeepc, &eeepc->bluetooth_rfkill,
435 "eeepc-bluetooth", RFKILL_TYPE_BLUETOOTH,
436 EEEPC_WMI_DEVID_BLUETOOTH);
437
438 if (result && result != -ENODEV)
439 goto exit;
440
441 result = eeepc_new_rfkill(eeepc, &eeepc->wwan3g_rfkill,
442 "eeepc-wwan3g", RFKILL_TYPE_WWAN,
443 EEEPC_WMI_DEVID_WWAN3G);
444
445 if (result && result != -ENODEV)
446 goto exit;
447
448exit:
449 if (result && result != -ENODEV)
450 eeepc_wmi_rfkill_exit(eeepc);
451
452 if (result == -ENODEV)
453 result = 0;
454
455 return result;
456}
457
458/*
Corentin Chary084fca62010-11-29 08:14:06 +0100459 * Backlight
460 */
Yong Wang3d7b1652010-04-11 09:27:54 +0800461static int read_brightness(struct backlight_device *bd)
462{
Corentin Charydfed65d2010-11-29 08:14:12 +0100463 u32 retval;
Yong Wang3d7b1652010-04-11 09:27:54 +0800464 acpi_status status;
465
Corentin Chary2a3f0062010-11-29 08:14:10 +0100466 status = eeepc_wmi_get_devstate(EEEPC_WMI_DEVID_BACKLIGHT, &retval);
Yong Wang3d7b1652010-04-11 09:27:54 +0800467
468 if (ACPI_FAILURE(status))
469 return -1;
470 else
Corentin Chary2a3f0062010-11-29 08:14:10 +0100471 return retval & 0xFF;
Yong Wang3d7b1652010-04-11 09:27:54 +0800472}
473
474static int update_bl_status(struct backlight_device *bd)
475{
476
Corentin Charydfed65d2010-11-29 08:14:12 +0100477 u32 ctrl_param;
Yong Wang3d7b1652010-04-11 09:27:54 +0800478 acpi_status status;
479
480 ctrl_param = bd->props.brightness;
481
Corentin Chary8c1b2d82010-11-29 08:14:09 +0100482 status = eeepc_wmi_set_devstate(EEEPC_WMI_DEVID_BACKLIGHT,
483 ctrl_param, NULL);
Yong Wang3d7b1652010-04-11 09:27:54 +0800484
485 if (ACPI_FAILURE(status))
486 return -1;
487 else
488 return 0;
489}
490
491static const struct backlight_ops eeepc_wmi_bl_ops = {
492 .get_brightness = read_brightness,
493 .update_status = update_bl_status,
494};
495
496static int eeepc_wmi_backlight_notify(struct eeepc_wmi *eeepc, int code)
497{
498 struct backlight_device *bd = eeepc->backlight_device;
499 int old = bd->props.brightness;
Daniel Mackb7670ed2010-05-19 12:37:01 +0200500 int new = old;
Yong Wang3d7b1652010-04-11 09:27:54 +0800501
502 if (code >= NOTIFY_BRNUP_MIN && code <= NOTIFY_BRNUP_MAX)
503 new = code - NOTIFY_BRNUP_MIN + 1;
504 else if (code >= NOTIFY_BRNDOWN_MIN && code <= NOTIFY_BRNDOWN_MAX)
505 new = code - NOTIFY_BRNDOWN_MIN;
506
507 bd->props.brightness = new;
508 backlight_update_status(bd);
509 backlight_force_update(bd, BACKLIGHT_UPDATE_HOTKEY);
510
511 return old;
512}
513
514static int eeepc_wmi_backlight_init(struct eeepc_wmi *eeepc)
515{
516 struct backlight_device *bd;
517 struct backlight_properties props;
518
519 memset(&props, 0, sizeof(struct backlight_properties));
520 props.max_brightness = 15;
521 bd = backlight_device_register(EEEPC_WMI_FILE,
Corentin Chary27c136c2010-11-29 08:14:05 +0100522 &eeepc->platform_device->dev, eeepc,
Yong Wang3d7b1652010-04-11 09:27:54 +0800523 &eeepc_wmi_bl_ops, &props);
524 if (IS_ERR(bd)) {
525 pr_err("Could not register backlight device\n");
526 return PTR_ERR(bd);
527 }
528
529 eeepc->backlight_device = bd;
530
531 bd->props.brightness = read_brightness(bd);
532 bd->props.power = FB_BLANK_UNBLANK;
533 backlight_update_status(bd);
534
535 return 0;
536}
537
538static void eeepc_wmi_backlight_exit(struct eeepc_wmi *eeepc)
539{
540 if (eeepc->backlight_device)
541 backlight_device_unregister(eeepc->backlight_device);
542
543 eeepc->backlight_device = NULL;
544}
545
546static void eeepc_wmi_notify(u32 value, void *context)
547{
548 struct eeepc_wmi *eeepc = context;
549 struct acpi_buffer response = { ACPI_ALLOCATE_BUFFER, NULL };
550 union acpi_object *obj;
551 acpi_status status;
552 int code;
553 int orig_code;
554
555 status = wmi_get_event_data(value, &response);
556 if (status != AE_OK) {
557 pr_err("bad event status 0x%x\n", status);
558 return;
559 }
560
561 obj = (union acpi_object *)response.pointer;
562
563 if (obj && obj->type == ACPI_TYPE_INTEGER) {
564 code = obj->integer.value;
565 orig_code = code;
566
567 if (code >= NOTIFY_BRNUP_MIN && code <= NOTIFY_BRNUP_MAX)
568 code = NOTIFY_BRNUP_MIN;
569 else if (code >= NOTIFY_BRNDOWN_MIN &&
570 code <= NOTIFY_BRNDOWN_MAX)
571 code = NOTIFY_BRNDOWN_MIN;
572
573 if (code == NOTIFY_BRNUP_MIN || code == NOTIFY_BRNDOWN_MIN) {
574 if (!acpi_video_backlight_support())
575 eeepc_wmi_backlight_notify(eeepc, orig_code);
576 }
577
578 if (!sparse_keymap_report_event(eeepc->inputdev,
579 code, 1, true))
580 pr_info("Unknown key %x pressed\n", code);
581 }
582
583 kfree(obj);
584}
585
Dmitry Torokhov67fa38e2010-11-03 11:14:01 -0700586static ssize_t store_cpufv(struct device *dev, struct device_attribute *attr,
587 const char *buf, size_t count)
Chris Bagwell7f80d732010-10-11 18:47:18 -0500588{
589 int value;
590 struct acpi_buffer input = { (acpi_size)sizeof(value), &value };
591 acpi_status status;
592
593 if (!count || sscanf(buf, "%i", &value) != 1)
594 return -EINVAL;
595 if (value < 0 || value > 2)
596 return -EINVAL;
597
598 status = wmi_evaluate_method(EEEPC_WMI_MGMT_GUID,
599 1, EEEPC_WMI_METHODID_CFVS, &input, NULL);
600
601 if (ACPI_FAILURE(status))
602 return -EIO;
603 else
604 return count;
605}
606
607static DEVICE_ATTR(cpufv, S_IRUGO | S_IWUSR, NULL, store_cpufv);
608
Corentin Chary4e37b422010-11-29 08:14:08 +0100609static struct attribute *platform_attributes[] = {
610 &dev_attr_cpufv.attr,
611 NULL
612};
613
614static struct attribute_group platform_attribute_group = {
615 .attrs = platform_attributes
616};
617
Chris Bagwell7f80d732010-10-11 18:47:18 -0500618static void eeepc_wmi_sysfs_exit(struct platform_device *device)
619{
Corentin Chary4e37b422010-11-29 08:14:08 +0100620 sysfs_remove_group(&device->dev.kobj, &platform_attribute_group);
Chris Bagwell7f80d732010-10-11 18:47:18 -0500621}
622
623static int eeepc_wmi_sysfs_init(struct platform_device *device)
624{
Corentin Chary4e37b422010-11-29 08:14:08 +0100625 return sysfs_create_group(&device->dev.kobj, &platform_attribute_group);
Chris Bagwell7f80d732010-10-11 18:47:18 -0500626}
627
Corentin Chary27c136c2010-11-29 08:14:05 +0100628/*
629 * Platform device
630 */
631static int __init eeepc_wmi_platform_init(struct eeepc_wmi *eeepc)
632{
633 int err;
634
635 eeepc->platform_device = platform_device_alloc(EEEPC_WMI_FILE, -1);
636 if (!eeepc->platform_device)
637 return -ENOMEM;
638 platform_set_drvdata(eeepc->platform_device, eeepc);
639
640 err = platform_device_add(eeepc->platform_device);
641 if (err)
642 goto fail_platform_device;
643
644 err = eeepc_wmi_sysfs_init(eeepc->platform_device);
645 if (err)
646 goto fail_sysfs;
647 return 0;
648
649fail_sysfs:
650 platform_device_del(eeepc->platform_device);
651fail_platform_device:
652 platform_device_put(eeepc->platform_device);
653 return err;
654}
655
656static void eeepc_wmi_platform_exit(struct eeepc_wmi *eeepc)
657{
658 eeepc_wmi_sysfs_exit(eeepc->platform_device);
659 platform_device_unregister(eeepc->platform_device);
660}
661
662/*
Corentin Chary8c1b2d82010-11-29 08:14:09 +0100663 * debugfs
664 */
665struct eeepc_wmi_debugfs_node {
666 struct eeepc_wmi *eeepc;
667 char *name;
668 int (*show)(struct seq_file *m, void *data);
669};
670
671static int show_dsts(struct seq_file *m, void *data)
672{
673 struct eeepc_wmi *eeepc = m->private;
674 acpi_status status;
675 u32 retval = -1;
676
677 status = eeepc_wmi_get_devstate(eeepc->debug.dev_id, &retval);
678
679 if (ACPI_FAILURE(status))
680 return -EIO;
681
682 seq_printf(m, "DSTS(%x) = %x\n", eeepc->debug.dev_id, retval);
683
684 return 0;
685}
686
687static int show_devs(struct seq_file *m, void *data)
688{
689 struct eeepc_wmi *eeepc = m->private;
690 acpi_status status;
691 u32 retval = -1;
692
693 status = eeepc_wmi_set_devstate(eeepc->debug.dev_id,
694 eeepc->debug.ctrl_param, &retval);
695 if (ACPI_FAILURE(status))
696 return -EIO;
697
698 seq_printf(m, "DEVS(%x, %x) = %x\n", eeepc->debug.dev_id,
699 eeepc->debug.ctrl_param, retval);
700
701 return 0;
702}
703
704static struct eeepc_wmi_debugfs_node eeepc_wmi_debug_files[] = {
705 { NULL, "devs", show_devs },
706 { NULL, "dsts", show_dsts },
707};
708
709static int eeepc_wmi_debugfs_open(struct inode *inode, struct file *file)
710{
711 struct eeepc_wmi_debugfs_node *node = inode->i_private;
712
713 return single_open(file, node->show, node->eeepc);
714}
715
716static const struct file_operations eeepc_wmi_debugfs_io_ops = {
717 .owner = THIS_MODULE,
718 .open = eeepc_wmi_debugfs_open,
719 .read = seq_read,
720 .llseek = seq_lseek,
721 .release = single_release,
722};
723
724static void eeepc_wmi_debugfs_exit(struct eeepc_wmi *eeepc)
725{
726 debugfs_remove_recursive(eeepc->debug.root);
727}
728
729static int eeepc_wmi_debugfs_init(struct eeepc_wmi *eeepc)
730{
731 struct dentry *dent;
732 int i;
733
734 eeepc->debug.root = debugfs_create_dir(EEEPC_WMI_FILE, NULL);
735 if (!eeepc->debug.root) {
736 pr_err("failed to create debugfs directory");
737 goto error_debugfs;
738 }
739
740 dent = debugfs_create_x32("dev_id", S_IRUGO|S_IWUSR,
741 eeepc->debug.root, &eeepc->debug.dev_id);
742 if (!dent)
743 goto error_debugfs;
744
745 dent = debugfs_create_x32("ctrl_param", S_IRUGO|S_IWUSR,
746 eeepc->debug.root, &eeepc->debug.ctrl_param);
747 if (!dent)
748 goto error_debugfs;
749
750 for (i = 0; i < ARRAY_SIZE(eeepc_wmi_debug_files); i++) {
751 struct eeepc_wmi_debugfs_node *node = &eeepc_wmi_debug_files[i];
752
753 node->eeepc = eeepc;
754 dent = debugfs_create_file(node->name, S_IFREG | S_IRUGO,
755 eeepc->debug.root, node,
756 &eeepc_wmi_debugfs_io_ops);
757 if (!dent) {
758 pr_err("failed to create debug file: %s\n", node->name);
759 goto error_debugfs;
760 }
761 }
762
763 return 0;
764
765error_debugfs:
766 eeepc_wmi_debugfs_exit(eeepc);
767 return -ENOMEM;
768}
769
770/*
Corentin Chary27c136c2010-11-29 08:14:05 +0100771 * WMI Driver
772 */
773static struct platform_device * __init eeepc_wmi_add(void)
Yong Wangee027e42010-03-21 10:26:34 +0800774{
Yong Wang45f2c692010-04-11 09:27:19 +0800775 struct eeepc_wmi *eeepc;
Yong Wangee027e42010-03-21 10:26:34 +0800776 acpi_status status;
Corentin Chary27c136c2010-11-29 08:14:05 +0100777 int err;
Yong Wangee027e42010-03-21 10:26:34 +0800778
Corentin Chary27c136c2010-11-29 08:14:05 +0100779 eeepc = kzalloc(sizeof(struct eeepc_wmi), GFP_KERNEL);
780 if (!eeepc)
781 return ERR_PTR(-ENOMEM);
782
783 /*
784 * Register the platform device first. It is used as a parent for the
785 * sub-devices below.
786 */
787 err = eeepc_wmi_platform_init(eeepc);
788 if (err)
789 goto fail_platform;
Yong Wang45f2c692010-04-11 09:27:19 +0800790
791 err = eeepc_wmi_input_init(eeepc);
792 if (err)
Corentin Chary27c136c2010-11-29 08:14:05 +0100793 goto fail_input;
Yong Wang3d7b1652010-04-11 09:27:54 +0800794
Corentin Chary084fca62010-11-29 08:14:06 +0100795 err = eeepc_wmi_led_init(eeepc);
796 if (err)
797 goto fail_leds;
798
Corentin Charyba48fdb2010-11-29 08:14:07 +0100799 err = eeepc_wmi_rfkill_init(eeepc);
800 if (err)
801 goto fail_rfkill;
802
Yong Wang3d7b1652010-04-11 09:27:54 +0800803 if (!acpi_video_backlight_support()) {
804 err = eeepc_wmi_backlight_init(eeepc);
805 if (err)
Corentin Chary27c136c2010-11-29 08:14:05 +0100806 goto fail_backlight;
Yong Wang3d7b1652010-04-11 09:27:54 +0800807 } else
808 pr_info("Backlight controlled by ACPI video driver\n");
Yong Wang45f2c692010-04-11 09:27:19 +0800809
810 status = wmi_install_notify_handler(EEEPC_WMI_EVENT_GUID,
Corentin Chary27c136c2010-11-29 08:14:05 +0100811 eeepc_wmi_notify, eeepc);
Yong Wang45f2c692010-04-11 09:27:19 +0800812 if (ACPI_FAILURE(status)) {
813 pr_err("Unable to register notify handler - %d\n",
814 status);
815 err = -ENODEV;
Corentin Chary27c136c2010-11-29 08:14:05 +0100816 goto fail_wmi_handler;
Yong Wang45f2c692010-04-11 09:27:19 +0800817 }
818
Corentin Chary8c1b2d82010-11-29 08:14:09 +0100819 err = eeepc_wmi_debugfs_init(eeepc);
820 if (err)
821 goto fail_debugfs;
822
Corentin Chary27c136c2010-11-29 08:14:05 +0100823 return eeepc->platform_device;
Yong Wang45f2c692010-04-11 09:27:19 +0800824
Corentin Chary8c1b2d82010-11-29 08:14:09 +0100825fail_debugfs:
826 wmi_remove_notify_handler(EEEPC_WMI_EVENT_GUID);
Corentin Chary27c136c2010-11-29 08:14:05 +0100827fail_wmi_handler:
Yong Wang3d7b1652010-04-11 09:27:54 +0800828 eeepc_wmi_backlight_exit(eeepc);
Corentin Chary27c136c2010-11-29 08:14:05 +0100829fail_backlight:
Corentin Charyba48fdb2010-11-29 08:14:07 +0100830 eeepc_wmi_rfkill_exit(eeepc);
831fail_rfkill:
Corentin Chary084fca62010-11-29 08:14:06 +0100832 eeepc_wmi_led_exit(eeepc);
833fail_leds:
Yong Wang45f2c692010-04-11 09:27:19 +0800834 eeepc_wmi_input_exit(eeepc);
Corentin Chary27c136c2010-11-29 08:14:05 +0100835fail_input:
836 eeepc_wmi_platform_exit(eeepc);
837fail_platform:
838 kfree(eeepc);
839 return ERR_PTR(err);
Yong Wang45f2c692010-04-11 09:27:19 +0800840}
841
Corentin Chary27c136c2010-11-29 08:14:05 +0100842static int eeepc_wmi_remove(struct platform_device *device)
Yong Wang45f2c692010-04-11 09:27:19 +0800843{
844 struct eeepc_wmi *eeepc;
845
846 eeepc = platform_get_drvdata(device);
847 wmi_remove_notify_handler(EEEPC_WMI_EVENT_GUID);
Yong Wang3d7b1652010-04-11 09:27:54 +0800848 eeepc_wmi_backlight_exit(eeepc);
Yong Wang45f2c692010-04-11 09:27:19 +0800849 eeepc_wmi_input_exit(eeepc);
Corentin Chary084fca62010-11-29 08:14:06 +0100850 eeepc_wmi_led_exit(eeepc);
Corentin Charyba48fdb2010-11-29 08:14:07 +0100851 eeepc_wmi_rfkill_exit(eeepc);
Corentin Chary8c1b2d82010-11-29 08:14:09 +0100852 eeepc_wmi_debugfs_exit(eeepc);
Corentin Chary27c136c2010-11-29 08:14:05 +0100853 eeepc_wmi_platform_exit(eeepc);
Yong Wang45f2c692010-04-11 09:27:19 +0800854
Corentin Chary27c136c2010-11-29 08:14:05 +0100855 kfree(eeepc);
Yong Wang45f2c692010-04-11 09:27:19 +0800856 return 0;
857}
858
859static struct platform_driver platform_driver = {
860 .driver = {
861 .name = EEEPC_WMI_FILE,
862 .owner = THIS_MODULE,
863 },
Yong Wang45f2c692010-04-11 09:27:19 +0800864};
865
Corentin Charyd358cb52010-11-29 08:14:14 +0100866static acpi_status __init eeepc_wmi_parse_device(acpi_handle handle, u32 level,
867 void *context, void **retval)
868{
869 pr_warning("Found legacy ATKD device (%s)", EEEPC_ACPI_HID);
870 *(bool *)context = true;
871 return AE_CTRL_TERMINATE;
872}
873
874static int __init eeepc_wmi_check_atkd(void)
875{
876 acpi_status status;
877 bool found = false;
878
879 status = acpi_get_devices(EEEPC_ACPI_HID, eeepc_wmi_parse_device,
880 &found, NULL);
881
882 if (ACPI_FAILURE(status) || !found)
883 return 0;
884 return -1;
885}
886
Yong Wang45f2c692010-04-11 09:27:19 +0800887static int __init eeepc_wmi_init(void)
888{
Yong Wang45f2c692010-04-11 09:27:19 +0800889 int err;
890
Yong Wang3d7b1652010-04-11 09:27:54 +0800891 if (!wmi_has_guid(EEEPC_WMI_EVENT_GUID) ||
892 !wmi_has_guid(EEEPC_WMI_MGMT_GUID)) {
Yong Wang81248882010-04-11 09:26:33 +0800893 pr_warning("No known WMI GUID found\n");
Yong Wangee027e42010-03-21 10:26:34 +0800894 return -ENODEV;
895 }
896
Corentin Charyd358cb52010-11-29 08:14:14 +0100897 if (eeepc_wmi_check_atkd()) {
898 pr_warning("WMI device present, but legacy ATKD device is also "
899 "present and enabled.");
900 pr_warning("You probably booted with acpi_osi=\"Linux\" or "
901 "acpi_osi=\"!Windows 2009\"");
902 pr_warning("Can't load eeepc-wmi, use default acpi_osi "
903 "(preferred) or eeepc-laptop");
904 return -ENODEV;
905 }
906
Corentin Chary27c136c2010-11-29 08:14:05 +0100907 platform_device = eeepc_wmi_add();
908 if (IS_ERR(platform_device)) {
909 err = PTR_ERR(platform_device);
910 goto fail_eeepc_wmi;
Yong Wang81248882010-04-11 09:26:33 +0800911 }
Yong Wangee027e42010-03-21 10:26:34 +0800912
Yong Wang45f2c692010-04-11 09:27:19 +0800913 err = platform_driver_register(&platform_driver);
914 if (err) {
915 pr_warning("Unable to register platform driver\n");
Corentin Chary27c136c2010-11-29 08:14:05 +0100916 goto fail_platform_driver;
Yong Wangee027e42010-03-21 10:26:34 +0800917 }
918
919 return 0;
Yong Wang45f2c692010-04-11 09:27:19 +0800920
Corentin Chary27c136c2010-11-29 08:14:05 +0100921fail_platform_driver:
922 eeepc_wmi_remove(platform_device);
923fail_eeepc_wmi:
Yong Wang45f2c692010-04-11 09:27:19 +0800924 return err;
Yong Wangee027e42010-03-21 10:26:34 +0800925}
926
927static void __exit eeepc_wmi_exit(void)
928{
Corentin Chary27c136c2010-11-29 08:14:05 +0100929 eeepc_wmi_remove(platform_device);
Yong Wang45f2c692010-04-11 09:27:19 +0800930 platform_driver_unregister(&platform_driver);
Yong Wangee027e42010-03-21 10:26:34 +0800931}
932
933module_init(eeepc_wmi_init);
934module_exit(eeepc_wmi_exit);