blob: 52274e21ee89e15bd116ee101a5bc66b63337f57 [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
52#define EEEPC_WMI_EVENT_GUID "ABBC0F72-8EA1-11D1-00A0-C90629100000"
Yong Wang3d7b1652010-04-11 09:27:54 +080053#define EEEPC_WMI_MGMT_GUID "97845ED0-4E6D-11DE-8A39-0800200C9A66"
Yong Wangee027e42010-03-21 10:26:34 +080054
55MODULE_ALIAS("wmi:"EEEPC_WMI_EVENT_GUID);
Yong Wang3d7b1652010-04-11 09:27:54 +080056MODULE_ALIAS("wmi:"EEEPC_WMI_MGMT_GUID);
Yong Wangee027e42010-03-21 10:26:34 +080057
58#define NOTIFY_BRNUP_MIN 0x11
59#define NOTIFY_BRNUP_MAX 0x1f
60#define NOTIFY_BRNDOWN_MIN 0x20
61#define NOTIFY_BRNDOWN_MAX 0x2e
62
Yong Wang3d7b1652010-04-11 09:27:54 +080063#define EEEPC_WMI_METHODID_DEVS 0x53564544
64#define EEEPC_WMI_METHODID_DSTS 0x53544344
Chris Bagwell7f80d732010-10-11 18:47:18 -050065#define EEEPC_WMI_METHODID_CFVS 0x53564643
Yong Wang3d7b1652010-04-11 09:27:54 +080066
67#define EEEPC_WMI_DEVID_BACKLIGHT 0x00050012
Corentin Chary084fca62010-11-29 08:14:06 +010068#define EEEPC_WMI_DEVID_TPDLED 0x00100011
Corentin Charyba48fdb2010-11-29 08:14:07 +010069#define EEEPC_WMI_DEVID_WLAN 0x00010011
70#define EEEPC_WMI_DEVID_BLUETOOTH 0x00010013
71#define EEEPC_WMI_DEVID_WWAN3G 0x00010019
Yong Wang3d7b1652010-04-11 09:27:54 +080072
Yong Wangee027e42010-03-21 10:26:34 +080073static const struct key_entry eeepc_wmi_keymap[] = {
74 /* Sleep already handled via generic ACPI code */
75 { KE_KEY, 0x5d, { KEY_WLAN } },
76 { KE_KEY, 0x32, { KEY_MUTE } },
77 { KE_KEY, 0x31, { KEY_VOLUMEDOWN } },
78 { KE_KEY, 0x30, { KEY_VOLUMEUP } },
79 { KE_IGNORE, NOTIFY_BRNDOWN_MIN, { KEY_BRIGHTNESSDOWN } },
80 { KE_IGNORE, NOTIFY_BRNUP_MIN, { KEY_BRIGHTNESSUP } },
81 { KE_KEY, 0xcc, { KEY_SWITCHVIDEOMODE } },
Chris Bagwelleda17482010-10-11 18:47:17 -050082 { KE_KEY, 0x6b, { KEY_F13 } }, /* Disable Touchpad */
83 { KE_KEY, 0xe1, { KEY_F14 } },
84 { KE_KEY, 0xe9, { KEY_DISPLAY_OFF } },
85 { KE_KEY, 0xe0, { KEY_PROG1 } },
86 { KE_KEY, 0x5c, { KEY_F15 } },
Yong Wangee027e42010-03-21 10:26:34 +080087 { KE_END, 0},
88};
89
Yong Wang3d7b1652010-04-11 09:27:54 +080090struct bios_args {
91 u32 dev_id;
92 u32 ctrl_param;
93};
94
Corentin Chary8c1b2d82010-11-29 08:14:09 +010095/*
96 * eeepc-wmi/ - debugfs root directory
97 * dev_id - current dev_id
98 * ctrl_param - current ctrl_param
99 * devs - call DEVS(dev_id, ctrl_param) and print result
100 * dsts - call DSTS(dev_id) and print result
101 */
102struct eeepc_wmi_debug {
103 struct dentry *root;
104 u32 dev_id;
105 u32 ctrl_param;
106};
107
Yong Wang81248882010-04-11 09:26:33 +0800108struct eeepc_wmi {
109 struct input_dev *inputdev;
Yong Wang3d7b1652010-04-11 09:27:54 +0800110 struct backlight_device *backlight_device;
Corentin Chary27c136c2010-11-29 08:14:05 +0100111 struct platform_device *platform_device;
Corentin Chary084fca62010-11-29 08:14:06 +0100112
113 struct led_classdev tpd_led;
114 int tpd_led_wk;
115 struct workqueue_struct *led_workqueue;
116 struct work_struct tpd_led_work;
Corentin Charyba48fdb2010-11-29 08:14:07 +0100117
118 struct rfkill *wlan_rfkill;
119 struct rfkill *bluetooth_rfkill;
120 struct rfkill *wwan3g_rfkill;
Corentin Chary8c1b2d82010-11-29 08:14:09 +0100121
122 struct eeepc_wmi_debug debug;
Yong Wang81248882010-04-11 09:26:33 +0800123};
124
Corentin Chary27c136c2010-11-29 08:14:05 +0100125/* Only used in eeepc_wmi_init() and eeepc_wmi_exit() */
Yong Wang45f2c692010-04-11 09:27:19 +0800126static struct platform_device *platform_device;
Yong Wangee027e42010-03-21 10:26:34 +0800127
Yong Wang81248882010-04-11 09:26:33 +0800128static int eeepc_wmi_input_init(struct eeepc_wmi *eeepc)
Yong Wangee027e42010-03-21 10:26:34 +0800129{
130 int err;
131
Yong Wang81248882010-04-11 09:26:33 +0800132 eeepc->inputdev = input_allocate_device();
133 if (!eeepc->inputdev)
Yong Wangee027e42010-03-21 10:26:34 +0800134 return -ENOMEM;
135
Yong Wang81248882010-04-11 09:26:33 +0800136 eeepc->inputdev->name = "Eee PC WMI hotkeys";
Yong Wang45f2c692010-04-11 09:27:19 +0800137 eeepc->inputdev->phys = EEEPC_WMI_FILE "/input0";
Yong Wang81248882010-04-11 09:26:33 +0800138 eeepc->inputdev->id.bustype = BUS_HOST;
Corentin Chary27c136c2010-11-29 08:14:05 +0100139 eeepc->inputdev->dev.parent = &eeepc->platform_device->dev;
Yong Wangee027e42010-03-21 10:26:34 +0800140
Yong Wang81248882010-04-11 09:26:33 +0800141 err = sparse_keymap_setup(eeepc->inputdev, eeepc_wmi_keymap, NULL);
Yong Wangee027e42010-03-21 10:26:34 +0800142 if (err)
143 goto err_free_dev;
144
Yong Wang81248882010-04-11 09:26:33 +0800145 err = input_register_device(eeepc->inputdev);
Yong Wangee027e42010-03-21 10:26:34 +0800146 if (err)
147 goto err_free_keymap;
148
149 return 0;
150
151err_free_keymap:
Yong Wang81248882010-04-11 09:26:33 +0800152 sparse_keymap_free(eeepc->inputdev);
Yong Wangee027e42010-03-21 10:26:34 +0800153err_free_dev:
Yong Wang81248882010-04-11 09:26:33 +0800154 input_free_device(eeepc->inputdev);
Yong Wangee027e42010-03-21 10:26:34 +0800155 return err;
156}
157
Yong Wang81248882010-04-11 09:26:33 +0800158static void eeepc_wmi_input_exit(struct eeepc_wmi *eeepc)
159{
160 if (eeepc->inputdev) {
161 sparse_keymap_free(eeepc->inputdev);
162 input_unregister_device(eeepc->inputdev);
163 }
164
165 eeepc->inputdev = NULL;
166}
167
Corentin Chary2a3f0062010-11-29 08:14:10 +0100168static acpi_status eeepc_wmi_get_devstate(u32 dev_id, u32 *retval)
Yong Wang3d7b1652010-04-11 09:27:54 +0800169{
170 struct acpi_buffer input = { (acpi_size)sizeof(u32), &dev_id };
171 struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER, NULL };
172 union acpi_object *obj;
173 acpi_status status;
174 u32 tmp;
175
176 status = wmi_evaluate_method(EEEPC_WMI_MGMT_GUID,
177 1, EEEPC_WMI_METHODID_DSTS, &input, &output);
178
179 if (ACPI_FAILURE(status))
180 return status;
181
182 obj = (union acpi_object *)output.pointer;
183 if (obj && obj->type == ACPI_TYPE_INTEGER)
184 tmp = (u32)obj->integer.value;
185 else
186 tmp = 0;
187
Corentin Chary2a3f0062010-11-29 08:14:10 +0100188 if (retval)
189 *retval = tmp;
Yong Wang3d7b1652010-04-11 09:27:54 +0800190
191 kfree(obj);
192
193 return status;
194
195}
196
Corentin Chary8c1b2d82010-11-29 08:14:09 +0100197static acpi_status eeepc_wmi_set_devstate(u32 dev_id, u32 ctrl_param,
198 u32 *retval)
Yong Wang3d7b1652010-04-11 09:27:54 +0800199{
200 struct bios_args args = {
201 .dev_id = dev_id,
202 .ctrl_param = ctrl_param,
203 };
204 struct acpi_buffer input = { (acpi_size)sizeof(args), &args };
205 acpi_status status;
206
Corentin Chary8c1b2d82010-11-29 08:14:09 +0100207 if (!retval) {
208 status = wmi_evaluate_method(EEEPC_WMI_MGMT_GUID, 1,
209 EEEPC_WMI_METHODID_DEVS,
210 &input, NULL);
211 } else {
212 struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER, NULL };
213 union acpi_object *obj;
214 u32 tmp;
215
216 status = wmi_evaluate_method(EEEPC_WMI_MGMT_GUID, 1,
217 EEEPC_WMI_METHODID_DEVS,
218 &input, &output);
219
220 if (ACPI_FAILURE(status))
221 return status;
222
223 obj = (union acpi_object *)output.pointer;
224 if (obj && obj->type == ACPI_TYPE_INTEGER)
225 tmp = (u32)obj->integer.value;
226 else
227 tmp = 0;
228
229 *retval = tmp;
230
231 kfree(obj);
232 }
Yong Wang3d7b1652010-04-11 09:27:54 +0800233
234 return status;
235}
236
Corentin Chary084fca62010-11-29 08:14:06 +0100237/*
238 * LEDs
239 */
240/*
241 * These functions actually update the LED's, and are called from a
242 * workqueue. By doing this as separate work rather than when the LED
243 * subsystem asks, we avoid messing with the Eeepc ACPI stuff during a
244 * potentially bad time, such as a timer interrupt.
245 */
246static void tpd_led_update(struct work_struct *work)
247{
248 int ctrl_param;
249 struct eeepc_wmi *eeepc;
250
251 eeepc = container_of(work, struct eeepc_wmi, tpd_led_work);
252
253 ctrl_param = eeepc->tpd_led_wk;
Corentin Chary8c1b2d82010-11-29 08:14:09 +0100254 eeepc_wmi_set_devstate(EEEPC_WMI_DEVID_TPDLED, ctrl_param, NULL);
Corentin Chary084fca62010-11-29 08:14:06 +0100255}
256
257static void tpd_led_set(struct led_classdev *led_cdev,
258 enum led_brightness value)
259{
260 struct eeepc_wmi *eeepc;
261
262 eeepc = container_of(led_cdev, struct eeepc_wmi, tpd_led);
263
264 eeepc->tpd_led_wk = !!value;
265 queue_work(eeepc->led_workqueue, &eeepc->tpd_led_work);
266}
267
268static int read_tpd_state(struct eeepc_wmi *eeepc)
269{
Corentin Charydfed65d2010-11-29 08:14:12 +0100270 u32 retval;
Corentin Chary084fca62010-11-29 08:14:06 +0100271 acpi_status status;
272
Corentin Chary2a3f0062010-11-29 08:14:10 +0100273 status = eeepc_wmi_get_devstate(EEEPC_WMI_DEVID_TPDLED, &retval);
Corentin Chary084fca62010-11-29 08:14:06 +0100274
275 if (ACPI_FAILURE(status))
276 return -1;
Corentin Chary2a3f0062010-11-29 08:14:10 +0100277 else if (!retval || retval == 0x00060000)
Corentin Chary084fca62010-11-29 08:14:06 +0100278 /*
279 * if touchpad led is present, DSTS will set some bits,
280 * usually 0x00020000.
281 * 0x00060000 means that the device is not supported
282 */
283 return -ENODEV;
284 else
285 /* Status is stored in the first bit */
Corentin Chary2a3f0062010-11-29 08:14:10 +0100286 return retval & 0x1;
Corentin Chary084fca62010-11-29 08:14:06 +0100287}
288
289static enum led_brightness tpd_led_get(struct led_classdev *led_cdev)
290{
291 struct eeepc_wmi *eeepc;
292
293 eeepc = container_of(led_cdev, struct eeepc_wmi, tpd_led);
294
295 return read_tpd_state(eeepc);
296}
297
298static int eeepc_wmi_led_init(struct eeepc_wmi *eeepc)
299{
300 int rv;
301
302 if (read_tpd_state(eeepc) < 0)
303 return 0;
304
305 eeepc->led_workqueue = create_singlethread_workqueue("led_workqueue");
306 if (!eeepc->led_workqueue)
307 return -ENOMEM;
308 INIT_WORK(&eeepc->tpd_led_work, tpd_led_update);
309
310 eeepc->tpd_led.name = "eeepc::touchpad";
311 eeepc->tpd_led.brightness_set = tpd_led_set;
312 eeepc->tpd_led.brightness_get = tpd_led_get;
313 eeepc->tpd_led.max_brightness = 1;
314
315 rv = led_classdev_register(&eeepc->platform_device->dev,
316 &eeepc->tpd_led);
317 if (rv) {
318 destroy_workqueue(eeepc->led_workqueue);
319 return rv;
320 }
321
322 return 0;
323}
324
325static void eeepc_wmi_led_exit(struct eeepc_wmi *eeepc)
326{
327 if (eeepc->tpd_led.dev)
328 led_classdev_unregister(&eeepc->tpd_led);
329 if (eeepc->led_workqueue)
330 destroy_workqueue(eeepc->led_workqueue);
331}
332
333/*
Corentin Charyba48fdb2010-11-29 08:14:07 +0100334 * Rfkill devices
335 */
336static int eeepc_rfkill_set(void *data, bool blocked)
337{
338 int dev_id = (unsigned long)data;
339 u32 ctrl_param = !blocked;
340
Corentin Chary8c1b2d82010-11-29 08:14:09 +0100341 return eeepc_wmi_set_devstate(dev_id, ctrl_param, NULL);
Corentin Charyba48fdb2010-11-29 08:14:07 +0100342}
343
344static void eeepc_rfkill_query(struct rfkill *rfkill, void *data)
345{
346 int dev_id = (unsigned long)data;
Corentin Chary2a3f0062010-11-29 08:14:10 +0100347 u32 retval;
Corentin Charyba48fdb2010-11-29 08:14:07 +0100348 acpi_status status;
349
Corentin Chary2a3f0062010-11-29 08:14:10 +0100350 status = eeepc_wmi_get_devstate(dev_id, &retval);
Corentin Charyba48fdb2010-11-29 08:14:07 +0100351
352 if (ACPI_FAILURE(status))
353 return ;
354
Corentin Chary2a3f0062010-11-29 08:14:10 +0100355 rfkill_set_sw_state(rfkill, !(retval & 0x1));
Corentin Charyba48fdb2010-11-29 08:14:07 +0100356}
357
358static const struct rfkill_ops eeepc_rfkill_ops = {
359 .set_block = eeepc_rfkill_set,
360 .query = eeepc_rfkill_query,
361};
362
363static int eeepc_new_rfkill(struct eeepc_wmi *eeepc,
364 struct rfkill **rfkill,
365 const char *name,
366 enum rfkill_type type, int dev_id)
367{
368 int result;
Corentin Chary2a3f0062010-11-29 08:14:10 +0100369 u32 retval;
Corentin Charyba48fdb2010-11-29 08:14:07 +0100370 acpi_status status;
371
Corentin Chary2a3f0062010-11-29 08:14:10 +0100372 status = eeepc_wmi_get_devstate(dev_id, &retval);
Corentin Charyba48fdb2010-11-29 08:14:07 +0100373
374 if (ACPI_FAILURE(status))
375 return -1;
376
377 /* If the device is present, DSTS will always set some bits
378 * 0x00070000 - 1110000000000000000 - device supported
379 * 0x00060000 - 1100000000000000000 - not supported
380 * 0x00020000 - 0100000000000000000 - device supported
381 * 0x00010000 - 0010000000000000000 - not supported / special mode ?
382 */
Corentin Chary2a3f0062010-11-29 08:14:10 +0100383 if (!retval || retval == 0x00060000)
Corentin Charyba48fdb2010-11-29 08:14:07 +0100384 return -ENODEV;
385
386 *rfkill = rfkill_alloc(name, &eeepc->platform_device->dev, type,
387 &eeepc_rfkill_ops, (void *)(long)dev_id);
388
389 if (!*rfkill)
390 return -EINVAL;
391
Corentin Chary2a3f0062010-11-29 08:14:10 +0100392 rfkill_init_sw_state(*rfkill, !(retval & 0x1));
Corentin Charyba48fdb2010-11-29 08:14:07 +0100393 result = rfkill_register(*rfkill);
394 if (result) {
395 rfkill_destroy(*rfkill);
396 *rfkill = NULL;
397 return result;
398 }
399 return 0;
400}
401
402static void eeepc_wmi_rfkill_exit(struct eeepc_wmi *eeepc)
403{
404 if (eeepc->wlan_rfkill) {
405 rfkill_unregister(eeepc->wlan_rfkill);
406 rfkill_destroy(eeepc->wlan_rfkill);
407 eeepc->wlan_rfkill = NULL;
408 }
409 if (eeepc->bluetooth_rfkill) {
410 rfkill_unregister(eeepc->bluetooth_rfkill);
411 rfkill_destroy(eeepc->bluetooth_rfkill);
412 eeepc->bluetooth_rfkill = NULL;
413 }
414 if (eeepc->wwan3g_rfkill) {
415 rfkill_unregister(eeepc->wwan3g_rfkill);
416 rfkill_destroy(eeepc->wwan3g_rfkill);
417 eeepc->wwan3g_rfkill = NULL;
418 }
419}
420
421static int eeepc_wmi_rfkill_init(struct eeepc_wmi *eeepc)
422{
423 int result = 0;
424
425 result = eeepc_new_rfkill(eeepc, &eeepc->wlan_rfkill,
426 "eeepc-wlan", RFKILL_TYPE_WLAN,
427 EEEPC_WMI_DEVID_WLAN);
428
429 if (result && result != -ENODEV)
430 goto exit;
431
432 result = eeepc_new_rfkill(eeepc, &eeepc->bluetooth_rfkill,
433 "eeepc-bluetooth", RFKILL_TYPE_BLUETOOTH,
434 EEEPC_WMI_DEVID_BLUETOOTH);
435
436 if (result && result != -ENODEV)
437 goto exit;
438
439 result = eeepc_new_rfkill(eeepc, &eeepc->wwan3g_rfkill,
440 "eeepc-wwan3g", RFKILL_TYPE_WWAN,
441 EEEPC_WMI_DEVID_WWAN3G);
442
443 if (result && result != -ENODEV)
444 goto exit;
445
446exit:
447 if (result && result != -ENODEV)
448 eeepc_wmi_rfkill_exit(eeepc);
449
450 if (result == -ENODEV)
451 result = 0;
452
453 return result;
454}
455
456/*
Corentin Chary084fca62010-11-29 08:14:06 +0100457 * Backlight
458 */
Yong Wang3d7b1652010-04-11 09:27:54 +0800459static int read_brightness(struct backlight_device *bd)
460{
Corentin Charydfed65d2010-11-29 08:14:12 +0100461 u32 retval;
Yong Wang3d7b1652010-04-11 09:27:54 +0800462 acpi_status status;
463
Corentin Chary2a3f0062010-11-29 08:14:10 +0100464 status = eeepc_wmi_get_devstate(EEEPC_WMI_DEVID_BACKLIGHT, &retval);
Yong Wang3d7b1652010-04-11 09:27:54 +0800465
466 if (ACPI_FAILURE(status))
467 return -1;
468 else
Corentin Chary2a3f0062010-11-29 08:14:10 +0100469 return retval & 0xFF;
Yong Wang3d7b1652010-04-11 09:27:54 +0800470}
471
472static int update_bl_status(struct backlight_device *bd)
473{
474
Corentin Charydfed65d2010-11-29 08:14:12 +0100475 u32 ctrl_param;
Yong Wang3d7b1652010-04-11 09:27:54 +0800476 acpi_status status;
477
478 ctrl_param = bd->props.brightness;
479
Corentin Chary8c1b2d82010-11-29 08:14:09 +0100480 status = eeepc_wmi_set_devstate(EEEPC_WMI_DEVID_BACKLIGHT,
481 ctrl_param, NULL);
Yong Wang3d7b1652010-04-11 09:27:54 +0800482
483 if (ACPI_FAILURE(status))
484 return -1;
485 else
486 return 0;
487}
488
489static const struct backlight_ops eeepc_wmi_bl_ops = {
490 .get_brightness = read_brightness,
491 .update_status = update_bl_status,
492};
493
494static int eeepc_wmi_backlight_notify(struct eeepc_wmi *eeepc, int code)
495{
496 struct backlight_device *bd = eeepc->backlight_device;
497 int old = bd->props.brightness;
Daniel Mackb7670ed2010-05-19 12:37:01 +0200498 int new = old;
Yong Wang3d7b1652010-04-11 09:27:54 +0800499
500 if (code >= NOTIFY_BRNUP_MIN && code <= NOTIFY_BRNUP_MAX)
501 new = code - NOTIFY_BRNUP_MIN + 1;
502 else if (code >= NOTIFY_BRNDOWN_MIN && code <= NOTIFY_BRNDOWN_MAX)
503 new = code - NOTIFY_BRNDOWN_MIN;
504
505 bd->props.brightness = new;
506 backlight_update_status(bd);
507 backlight_force_update(bd, BACKLIGHT_UPDATE_HOTKEY);
508
509 return old;
510}
511
512static int eeepc_wmi_backlight_init(struct eeepc_wmi *eeepc)
513{
514 struct backlight_device *bd;
515 struct backlight_properties props;
516
517 memset(&props, 0, sizeof(struct backlight_properties));
518 props.max_brightness = 15;
519 bd = backlight_device_register(EEEPC_WMI_FILE,
Corentin Chary27c136c2010-11-29 08:14:05 +0100520 &eeepc->platform_device->dev, eeepc,
Yong Wang3d7b1652010-04-11 09:27:54 +0800521 &eeepc_wmi_bl_ops, &props);
522 if (IS_ERR(bd)) {
523 pr_err("Could not register backlight device\n");
524 return PTR_ERR(bd);
525 }
526
527 eeepc->backlight_device = bd;
528
529 bd->props.brightness = read_brightness(bd);
530 bd->props.power = FB_BLANK_UNBLANK;
531 backlight_update_status(bd);
532
533 return 0;
534}
535
536static void eeepc_wmi_backlight_exit(struct eeepc_wmi *eeepc)
537{
538 if (eeepc->backlight_device)
539 backlight_device_unregister(eeepc->backlight_device);
540
541 eeepc->backlight_device = NULL;
542}
543
544static void eeepc_wmi_notify(u32 value, void *context)
545{
546 struct eeepc_wmi *eeepc = context;
547 struct acpi_buffer response = { ACPI_ALLOCATE_BUFFER, NULL };
548 union acpi_object *obj;
549 acpi_status status;
550 int code;
551 int orig_code;
552
553 status = wmi_get_event_data(value, &response);
554 if (status != AE_OK) {
555 pr_err("bad event status 0x%x\n", status);
556 return;
557 }
558
559 obj = (union acpi_object *)response.pointer;
560
561 if (obj && obj->type == ACPI_TYPE_INTEGER) {
562 code = obj->integer.value;
563 orig_code = code;
564
565 if (code >= NOTIFY_BRNUP_MIN && code <= NOTIFY_BRNUP_MAX)
566 code = NOTIFY_BRNUP_MIN;
567 else if (code >= NOTIFY_BRNDOWN_MIN &&
568 code <= NOTIFY_BRNDOWN_MAX)
569 code = NOTIFY_BRNDOWN_MIN;
570
571 if (code == NOTIFY_BRNUP_MIN || code == NOTIFY_BRNDOWN_MIN) {
572 if (!acpi_video_backlight_support())
573 eeepc_wmi_backlight_notify(eeepc, orig_code);
574 }
575
576 if (!sparse_keymap_report_event(eeepc->inputdev,
577 code, 1, true))
578 pr_info("Unknown key %x pressed\n", code);
579 }
580
581 kfree(obj);
582}
583
Dmitry Torokhov67fa38e2010-11-03 11:14:01 -0700584static ssize_t store_cpufv(struct device *dev, struct device_attribute *attr,
585 const char *buf, size_t count)
Chris Bagwell7f80d732010-10-11 18:47:18 -0500586{
587 int value;
588 struct acpi_buffer input = { (acpi_size)sizeof(value), &value };
589 acpi_status status;
590
591 if (!count || sscanf(buf, "%i", &value) != 1)
592 return -EINVAL;
593 if (value < 0 || value > 2)
594 return -EINVAL;
595
596 status = wmi_evaluate_method(EEEPC_WMI_MGMT_GUID,
597 1, EEEPC_WMI_METHODID_CFVS, &input, NULL);
598
599 if (ACPI_FAILURE(status))
600 return -EIO;
601 else
602 return count;
603}
604
605static DEVICE_ATTR(cpufv, S_IRUGO | S_IWUSR, NULL, store_cpufv);
606
Corentin Chary4e37b422010-11-29 08:14:08 +0100607static struct attribute *platform_attributes[] = {
608 &dev_attr_cpufv.attr,
609 NULL
610};
611
612static struct attribute_group platform_attribute_group = {
613 .attrs = platform_attributes
614};
615
Chris Bagwell7f80d732010-10-11 18:47:18 -0500616static void eeepc_wmi_sysfs_exit(struct platform_device *device)
617{
Corentin Chary4e37b422010-11-29 08:14:08 +0100618 sysfs_remove_group(&device->dev.kobj, &platform_attribute_group);
Chris Bagwell7f80d732010-10-11 18:47:18 -0500619}
620
621static int eeepc_wmi_sysfs_init(struct platform_device *device)
622{
Corentin Chary4e37b422010-11-29 08:14:08 +0100623 return sysfs_create_group(&device->dev.kobj, &platform_attribute_group);
Chris Bagwell7f80d732010-10-11 18:47:18 -0500624}
625
Corentin Chary27c136c2010-11-29 08:14:05 +0100626/*
627 * Platform device
628 */
629static int __init eeepc_wmi_platform_init(struct eeepc_wmi *eeepc)
630{
631 int err;
632
633 eeepc->platform_device = platform_device_alloc(EEEPC_WMI_FILE, -1);
634 if (!eeepc->platform_device)
635 return -ENOMEM;
636 platform_set_drvdata(eeepc->platform_device, eeepc);
637
638 err = platform_device_add(eeepc->platform_device);
639 if (err)
640 goto fail_platform_device;
641
642 err = eeepc_wmi_sysfs_init(eeepc->platform_device);
643 if (err)
644 goto fail_sysfs;
645 return 0;
646
647fail_sysfs:
648 platform_device_del(eeepc->platform_device);
649fail_platform_device:
650 platform_device_put(eeepc->platform_device);
651 return err;
652}
653
654static void eeepc_wmi_platform_exit(struct eeepc_wmi *eeepc)
655{
656 eeepc_wmi_sysfs_exit(eeepc->platform_device);
657 platform_device_unregister(eeepc->platform_device);
658}
659
660/*
Corentin Chary8c1b2d82010-11-29 08:14:09 +0100661 * debugfs
662 */
663struct eeepc_wmi_debugfs_node {
664 struct eeepc_wmi *eeepc;
665 char *name;
666 int (*show)(struct seq_file *m, void *data);
667};
668
669static int show_dsts(struct seq_file *m, void *data)
670{
671 struct eeepc_wmi *eeepc = m->private;
672 acpi_status status;
673 u32 retval = -1;
674
675 status = eeepc_wmi_get_devstate(eeepc->debug.dev_id, &retval);
676
677 if (ACPI_FAILURE(status))
678 return -EIO;
679
680 seq_printf(m, "DSTS(%x) = %x\n", eeepc->debug.dev_id, retval);
681
682 return 0;
683}
684
685static int show_devs(struct seq_file *m, void *data)
686{
687 struct eeepc_wmi *eeepc = m->private;
688 acpi_status status;
689 u32 retval = -1;
690
691 status = eeepc_wmi_set_devstate(eeepc->debug.dev_id,
692 eeepc->debug.ctrl_param, &retval);
693 if (ACPI_FAILURE(status))
694 return -EIO;
695
696 seq_printf(m, "DEVS(%x, %x) = %x\n", eeepc->debug.dev_id,
697 eeepc->debug.ctrl_param, retval);
698
699 return 0;
700}
701
702static struct eeepc_wmi_debugfs_node eeepc_wmi_debug_files[] = {
703 { NULL, "devs", show_devs },
704 { NULL, "dsts", show_dsts },
705};
706
707static int eeepc_wmi_debugfs_open(struct inode *inode, struct file *file)
708{
709 struct eeepc_wmi_debugfs_node *node = inode->i_private;
710
711 return single_open(file, node->show, node->eeepc);
712}
713
714static const struct file_operations eeepc_wmi_debugfs_io_ops = {
715 .owner = THIS_MODULE,
716 .open = eeepc_wmi_debugfs_open,
717 .read = seq_read,
718 .llseek = seq_lseek,
719 .release = single_release,
720};
721
722static void eeepc_wmi_debugfs_exit(struct eeepc_wmi *eeepc)
723{
724 debugfs_remove_recursive(eeepc->debug.root);
725}
726
727static int eeepc_wmi_debugfs_init(struct eeepc_wmi *eeepc)
728{
729 struct dentry *dent;
730 int i;
731
732 eeepc->debug.root = debugfs_create_dir(EEEPC_WMI_FILE, NULL);
733 if (!eeepc->debug.root) {
734 pr_err("failed to create debugfs directory");
735 goto error_debugfs;
736 }
737
738 dent = debugfs_create_x32("dev_id", S_IRUGO|S_IWUSR,
739 eeepc->debug.root, &eeepc->debug.dev_id);
740 if (!dent)
741 goto error_debugfs;
742
743 dent = debugfs_create_x32("ctrl_param", S_IRUGO|S_IWUSR,
744 eeepc->debug.root, &eeepc->debug.ctrl_param);
745 if (!dent)
746 goto error_debugfs;
747
748 for (i = 0; i < ARRAY_SIZE(eeepc_wmi_debug_files); i++) {
749 struct eeepc_wmi_debugfs_node *node = &eeepc_wmi_debug_files[i];
750
751 node->eeepc = eeepc;
752 dent = debugfs_create_file(node->name, S_IFREG | S_IRUGO,
753 eeepc->debug.root, node,
754 &eeepc_wmi_debugfs_io_ops);
755 if (!dent) {
756 pr_err("failed to create debug file: %s\n", node->name);
757 goto error_debugfs;
758 }
759 }
760
761 return 0;
762
763error_debugfs:
764 eeepc_wmi_debugfs_exit(eeepc);
765 return -ENOMEM;
766}
767
768/*
Corentin Chary27c136c2010-11-29 08:14:05 +0100769 * WMI Driver
770 */
771static struct platform_device * __init eeepc_wmi_add(void)
Yong Wangee027e42010-03-21 10:26:34 +0800772{
Yong Wang45f2c692010-04-11 09:27:19 +0800773 struct eeepc_wmi *eeepc;
Yong Wangee027e42010-03-21 10:26:34 +0800774 acpi_status status;
Corentin Chary27c136c2010-11-29 08:14:05 +0100775 int err;
Yong Wangee027e42010-03-21 10:26:34 +0800776
Corentin Chary27c136c2010-11-29 08:14:05 +0100777 eeepc = kzalloc(sizeof(struct eeepc_wmi), GFP_KERNEL);
778 if (!eeepc)
779 return ERR_PTR(-ENOMEM);
780
781 /*
782 * Register the platform device first. It is used as a parent for the
783 * sub-devices below.
784 */
785 err = eeepc_wmi_platform_init(eeepc);
786 if (err)
787 goto fail_platform;
Yong Wang45f2c692010-04-11 09:27:19 +0800788
789 err = eeepc_wmi_input_init(eeepc);
790 if (err)
Corentin Chary27c136c2010-11-29 08:14:05 +0100791 goto fail_input;
Yong Wang3d7b1652010-04-11 09:27:54 +0800792
Corentin Chary084fca62010-11-29 08:14:06 +0100793 err = eeepc_wmi_led_init(eeepc);
794 if (err)
795 goto fail_leds;
796
Corentin Charyba48fdb2010-11-29 08:14:07 +0100797 err = eeepc_wmi_rfkill_init(eeepc);
798 if (err)
799 goto fail_rfkill;
800
Yong Wang3d7b1652010-04-11 09:27:54 +0800801 if (!acpi_video_backlight_support()) {
802 err = eeepc_wmi_backlight_init(eeepc);
803 if (err)
Corentin Chary27c136c2010-11-29 08:14:05 +0100804 goto fail_backlight;
Yong Wang3d7b1652010-04-11 09:27:54 +0800805 } else
806 pr_info("Backlight controlled by ACPI video driver\n");
Yong Wang45f2c692010-04-11 09:27:19 +0800807
808 status = wmi_install_notify_handler(EEEPC_WMI_EVENT_GUID,
Corentin Chary27c136c2010-11-29 08:14:05 +0100809 eeepc_wmi_notify, eeepc);
Yong Wang45f2c692010-04-11 09:27:19 +0800810 if (ACPI_FAILURE(status)) {
811 pr_err("Unable to register notify handler - %d\n",
812 status);
813 err = -ENODEV;
Corentin Chary27c136c2010-11-29 08:14:05 +0100814 goto fail_wmi_handler;
Yong Wang45f2c692010-04-11 09:27:19 +0800815 }
816
Corentin Chary8c1b2d82010-11-29 08:14:09 +0100817 err = eeepc_wmi_debugfs_init(eeepc);
818 if (err)
819 goto fail_debugfs;
820
Corentin Chary27c136c2010-11-29 08:14:05 +0100821 return eeepc->platform_device;
Yong Wang45f2c692010-04-11 09:27:19 +0800822
Corentin Chary8c1b2d82010-11-29 08:14:09 +0100823fail_debugfs:
824 wmi_remove_notify_handler(EEEPC_WMI_EVENT_GUID);
Corentin Chary27c136c2010-11-29 08:14:05 +0100825fail_wmi_handler:
Yong Wang3d7b1652010-04-11 09:27:54 +0800826 eeepc_wmi_backlight_exit(eeepc);
Corentin Chary27c136c2010-11-29 08:14:05 +0100827fail_backlight:
Corentin Charyba48fdb2010-11-29 08:14:07 +0100828 eeepc_wmi_rfkill_exit(eeepc);
829fail_rfkill:
Corentin Chary084fca62010-11-29 08:14:06 +0100830 eeepc_wmi_led_exit(eeepc);
831fail_leds:
Yong Wang45f2c692010-04-11 09:27:19 +0800832 eeepc_wmi_input_exit(eeepc);
Corentin Chary27c136c2010-11-29 08:14:05 +0100833fail_input:
834 eeepc_wmi_platform_exit(eeepc);
835fail_platform:
836 kfree(eeepc);
837 return ERR_PTR(err);
Yong Wang45f2c692010-04-11 09:27:19 +0800838}
839
Corentin Chary27c136c2010-11-29 08:14:05 +0100840static int eeepc_wmi_remove(struct platform_device *device)
Yong Wang45f2c692010-04-11 09:27:19 +0800841{
842 struct eeepc_wmi *eeepc;
843
844 eeepc = platform_get_drvdata(device);
845 wmi_remove_notify_handler(EEEPC_WMI_EVENT_GUID);
Yong Wang3d7b1652010-04-11 09:27:54 +0800846 eeepc_wmi_backlight_exit(eeepc);
Yong Wang45f2c692010-04-11 09:27:19 +0800847 eeepc_wmi_input_exit(eeepc);
Corentin Chary084fca62010-11-29 08:14:06 +0100848 eeepc_wmi_led_exit(eeepc);
Corentin Charyba48fdb2010-11-29 08:14:07 +0100849 eeepc_wmi_rfkill_exit(eeepc);
Corentin Chary8c1b2d82010-11-29 08:14:09 +0100850 eeepc_wmi_debugfs_exit(eeepc);
Corentin Chary27c136c2010-11-29 08:14:05 +0100851 eeepc_wmi_platform_exit(eeepc);
Yong Wang45f2c692010-04-11 09:27:19 +0800852
Corentin Chary27c136c2010-11-29 08:14:05 +0100853 kfree(eeepc);
Yong Wang45f2c692010-04-11 09:27:19 +0800854 return 0;
855}
856
857static struct platform_driver platform_driver = {
858 .driver = {
859 .name = EEEPC_WMI_FILE,
860 .owner = THIS_MODULE,
861 },
Yong Wang45f2c692010-04-11 09:27:19 +0800862};
863
864static int __init eeepc_wmi_init(void)
865{
Yong Wang45f2c692010-04-11 09:27:19 +0800866 int err;
867
Yong Wang3d7b1652010-04-11 09:27:54 +0800868 if (!wmi_has_guid(EEEPC_WMI_EVENT_GUID) ||
869 !wmi_has_guid(EEEPC_WMI_MGMT_GUID)) {
Yong Wang81248882010-04-11 09:26:33 +0800870 pr_warning("No known WMI GUID found\n");
Yong Wangee027e42010-03-21 10:26:34 +0800871 return -ENODEV;
872 }
873
Corentin Chary27c136c2010-11-29 08:14:05 +0100874 platform_device = eeepc_wmi_add();
875 if (IS_ERR(platform_device)) {
876 err = PTR_ERR(platform_device);
877 goto fail_eeepc_wmi;
Yong Wang81248882010-04-11 09:26:33 +0800878 }
Yong Wangee027e42010-03-21 10:26:34 +0800879
Yong Wang45f2c692010-04-11 09:27:19 +0800880 err = platform_driver_register(&platform_driver);
881 if (err) {
882 pr_warning("Unable to register platform driver\n");
Corentin Chary27c136c2010-11-29 08:14:05 +0100883 goto fail_platform_driver;
Yong Wangee027e42010-03-21 10:26:34 +0800884 }
885
886 return 0;
Yong Wang45f2c692010-04-11 09:27:19 +0800887
Corentin Chary27c136c2010-11-29 08:14:05 +0100888fail_platform_driver:
889 eeepc_wmi_remove(platform_device);
890fail_eeepc_wmi:
Yong Wang45f2c692010-04-11 09:27:19 +0800891 return err;
Yong Wangee027e42010-03-21 10:26:34 +0800892}
893
894static void __exit eeepc_wmi_exit(void)
895{
Corentin Chary27c136c2010-11-29 08:14:05 +0100896 eeepc_wmi_remove(platform_device);
Yong Wang45f2c692010-04-11 09:27:19 +0800897 platform_driver_unregister(&platform_driver);
Yong Wangee027e42010-03-21 10:26:34 +0800898}
899
900module_init(eeepc_wmi_init);
901module_exit(eeepc_wmi_exit);