blob: 253070ab963c32c1d4efb38815c03d9aff67f90e [file] [log] [blame]
Yong Wangee027e42010-03-21 10:26:34 +08001/*
2 * Eee PC WMI hotkey driver
3 *
4 * Copyright(C) 2010 Intel Corporation.
5 *
6 * Portions based on wistron_btns.c:
7 * Copyright (C) 2005 Miloslav Trmac <mitr@volny.cz>
8 * Copyright (C) 2005 Bernhard Rosenkraenzer <bero@arklinux.org>
9 * Copyright (C) 2005 Dmitry Torokhov <dtor@mail.ru>
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 */
25
Yong Wang81248882010-04-11 09:26:33 +080026#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
27
Yong Wangee027e42010-03-21 10:26:34 +080028#include <linux/kernel.h>
29#include <linux/module.h>
30#include <linux/init.h>
31#include <linux/types.h>
Tejun Heoa32f3922010-04-05 11:37:59 +090032#include <linux/slab.h>
Yong Wangee027e42010-03-21 10:26:34 +080033#include <linux/input.h>
34#include <linux/input/sparse-keymap.h>
Yong Wang3d7b1652010-04-11 09:27:54 +080035#include <linux/fb.h>
36#include <linux/backlight.h>
Corentin Chary084fca62010-11-29 08:14:06 +010037#include <linux/leds.h>
Corentin Charyba48fdb2010-11-29 08:14:07 +010038#include <linux/rfkill.h>
Corentin Chary8c1b2d82010-11-29 08:14:09 +010039#include <linux/debugfs.h>
40#include <linux/seq_file.h>
Yong Wang45f2c692010-04-11 09:27:19 +080041#include <linux/platform_device.h>
Yong Wangee027e42010-03-21 10:26:34 +080042#include <acpi/acpi_bus.h>
43#include <acpi/acpi_drivers.h>
44
Yong Wang45f2c692010-04-11 09:27:19 +080045#define EEEPC_WMI_FILE "eeepc-wmi"
46
Yong Wangee027e42010-03-21 10:26:34 +080047MODULE_AUTHOR("Yong Wang <yong.y.wang@intel.com>");
48MODULE_DESCRIPTION("Eee PC WMI Hotkey Driver");
49MODULE_LICENSE("GPL");
50
51#define EEEPC_WMI_EVENT_GUID "ABBC0F72-8EA1-11D1-00A0-C90629100000"
Yong Wang3d7b1652010-04-11 09:27:54 +080052#define EEEPC_WMI_MGMT_GUID "97845ED0-4E6D-11DE-8A39-0800200C9A66"
Yong Wangee027e42010-03-21 10:26:34 +080053
54MODULE_ALIAS("wmi:"EEEPC_WMI_EVENT_GUID);
Yong Wang3d7b1652010-04-11 09:27:54 +080055MODULE_ALIAS("wmi:"EEEPC_WMI_MGMT_GUID);
Yong Wangee027e42010-03-21 10:26:34 +080056
57#define NOTIFY_BRNUP_MIN 0x11
58#define NOTIFY_BRNUP_MAX 0x1f
59#define NOTIFY_BRNDOWN_MIN 0x20
60#define NOTIFY_BRNDOWN_MAX 0x2e
61
Yong Wang3d7b1652010-04-11 09:27:54 +080062#define EEEPC_WMI_METHODID_DEVS 0x53564544
63#define EEEPC_WMI_METHODID_DSTS 0x53544344
Chris Bagwell7f80d732010-10-11 18:47:18 -050064#define EEEPC_WMI_METHODID_CFVS 0x53564643
Yong Wang3d7b1652010-04-11 09:27:54 +080065
66#define EEEPC_WMI_DEVID_BACKLIGHT 0x00050012
Corentin Chary084fca62010-11-29 08:14:06 +010067#define EEEPC_WMI_DEVID_TPDLED 0x00100011
Corentin Charyba48fdb2010-11-29 08:14:07 +010068#define EEEPC_WMI_DEVID_WLAN 0x00010011
69#define EEEPC_WMI_DEVID_BLUETOOTH 0x00010013
70#define EEEPC_WMI_DEVID_WWAN3G 0x00010019
Yong Wang3d7b1652010-04-11 09:27:54 +080071
Yong Wangee027e42010-03-21 10:26:34 +080072static const struct key_entry eeepc_wmi_keymap[] = {
73 /* Sleep already handled via generic ACPI code */
74 { KE_KEY, 0x5d, { KEY_WLAN } },
75 { KE_KEY, 0x32, { KEY_MUTE } },
76 { KE_KEY, 0x31, { KEY_VOLUMEDOWN } },
77 { KE_KEY, 0x30, { KEY_VOLUMEUP } },
78 { KE_IGNORE, NOTIFY_BRNDOWN_MIN, { KEY_BRIGHTNESSDOWN } },
79 { KE_IGNORE, NOTIFY_BRNUP_MIN, { KEY_BRIGHTNESSUP } },
80 { KE_KEY, 0xcc, { KEY_SWITCHVIDEOMODE } },
Chris Bagwelleda17482010-10-11 18:47:17 -050081 { KE_KEY, 0x6b, { KEY_F13 } }, /* Disable Touchpad */
82 { KE_KEY, 0xe1, { KEY_F14 } },
83 { KE_KEY, 0xe9, { KEY_DISPLAY_OFF } },
84 { KE_KEY, 0xe0, { KEY_PROG1 } },
85 { KE_KEY, 0x5c, { KEY_F15 } },
Yong Wangee027e42010-03-21 10:26:34 +080086 { KE_END, 0},
87};
88
Yong Wang3d7b1652010-04-11 09:27:54 +080089struct bios_args {
90 u32 dev_id;
91 u32 ctrl_param;
92};
93
Corentin Chary8c1b2d82010-11-29 08:14:09 +010094/*
95 * eeepc-wmi/ - debugfs root directory
96 * dev_id - current dev_id
97 * ctrl_param - current ctrl_param
98 * devs - call DEVS(dev_id, ctrl_param) and print result
99 * dsts - call DSTS(dev_id) and print result
100 */
101struct eeepc_wmi_debug {
102 struct dentry *root;
103 u32 dev_id;
104 u32 ctrl_param;
105};
106
Yong Wang81248882010-04-11 09:26:33 +0800107struct eeepc_wmi {
108 struct input_dev *inputdev;
Yong Wang3d7b1652010-04-11 09:27:54 +0800109 struct backlight_device *backlight_device;
Corentin Chary27c136c2010-11-29 08:14:05 +0100110 struct platform_device *platform_device;
Corentin Chary084fca62010-11-29 08:14:06 +0100111
112 struct led_classdev tpd_led;
113 int tpd_led_wk;
114 struct workqueue_struct *led_workqueue;
115 struct work_struct tpd_led_work;
Corentin Charyba48fdb2010-11-29 08:14:07 +0100116
117 struct rfkill *wlan_rfkill;
118 struct rfkill *bluetooth_rfkill;
119 struct rfkill *wwan3g_rfkill;
Corentin Chary8c1b2d82010-11-29 08:14:09 +0100120
121 struct eeepc_wmi_debug debug;
Yong Wang81248882010-04-11 09:26:33 +0800122};
123
Corentin Chary27c136c2010-11-29 08:14:05 +0100124/* Only used in eeepc_wmi_init() and eeepc_wmi_exit() */
Yong Wang45f2c692010-04-11 09:27:19 +0800125static struct platform_device *platform_device;
Yong Wangee027e42010-03-21 10:26:34 +0800126
Yong Wang81248882010-04-11 09:26:33 +0800127static int eeepc_wmi_input_init(struct eeepc_wmi *eeepc)
Yong Wangee027e42010-03-21 10:26:34 +0800128{
129 int err;
130
Yong Wang81248882010-04-11 09:26:33 +0800131 eeepc->inputdev = input_allocate_device();
132 if (!eeepc->inputdev)
Yong Wangee027e42010-03-21 10:26:34 +0800133 return -ENOMEM;
134
Yong Wang81248882010-04-11 09:26:33 +0800135 eeepc->inputdev->name = "Eee PC WMI hotkeys";
Yong Wang45f2c692010-04-11 09:27:19 +0800136 eeepc->inputdev->phys = EEEPC_WMI_FILE "/input0";
Yong Wang81248882010-04-11 09:26:33 +0800137 eeepc->inputdev->id.bustype = BUS_HOST;
Corentin Chary27c136c2010-11-29 08:14:05 +0100138 eeepc->inputdev->dev.parent = &eeepc->platform_device->dev;
Yong Wangee027e42010-03-21 10:26:34 +0800139
Yong Wang81248882010-04-11 09:26:33 +0800140 err = sparse_keymap_setup(eeepc->inputdev, eeepc_wmi_keymap, NULL);
Yong Wangee027e42010-03-21 10:26:34 +0800141 if (err)
142 goto err_free_dev;
143
Yong Wang81248882010-04-11 09:26:33 +0800144 err = input_register_device(eeepc->inputdev);
Yong Wangee027e42010-03-21 10:26:34 +0800145 if (err)
146 goto err_free_keymap;
147
148 return 0;
149
150err_free_keymap:
Yong Wang81248882010-04-11 09:26:33 +0800151 sparse_keymap_free(eeepc->inputdev);
Yong Wangee027e42010-03-21 10:26:34 +0800152err_free_dev:
Yong Wang81248882010-04-11 09:26:33 +0800153 input_free_device(eeepc->inputdev);
Yong Wangee027e42010-03-21 10:26:34 +0800154 return err;
155}
156
Yong Wang81248882010-04-11 09:26:33 +0800157static void eeepc_wmi_input_exit(struct eeepc_wmi *eeepc)
158{
159 if (eeepc->inputdev) {
160 sparse_keymap_free(eeepc->inputdev);
161 input_unregister_device(eeepc->inputdev);
162 }
163
164 eeepc->inputdev = NULL;
165}
166
Yong Wang3d7b1652010-04-11 09:27:54 +0800167static acpi_status eeepc_wmi_get_devstate(u32 dev_id, u32 *ctrl_param)
168{
169 struct acpi_buffer input = { (acpi_size)sizeof(u32), &dev_id };
170 struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER, NULL };
171 union acpi_object *obj;
172 acpi_status status;
173 u32 tmp;
174
175 status = wmi_evaluate_method(EEEPC_WMI_MGMT_GUID,
176 1, EEEPC_WMI_METHODID_DSTS, &input, &output);
177
178 if (ACPI_FAILURE(status))
179 return status;
180
181 obj = (union acpi_object *)output.pointer;
182 if (obj && obj->type == ACPI_TYPE_INTEGER)
183 tmp = (u32)obj->integer.value;
184 else
185 tmp = 0;
186
187 if (ctrl_param)
188 *ctrl_param = tmp;
189
190 kfree(obj);
191
192 return status;
193
194}
195
Corentin Chary8c1b2d82010-11-29 08:14:09 +0100196static acpi_status eeepc_wmi_set_devstate(u32 dev_id, u32 ctrl_param,
197 u32 *retval)
Yong Wang3d7b1652010-04-11 09:27:54 +0800198{
199 struct bios_args args = {
200 .dev_id = dev_id,
201 .ctrl_param = ctrl_param,
202 };
203 struct acpi_buffer input = { (acpi_size)sizeof(args), &args };
204 acpi_status status;
205
Corentin Chary8c1b2d82010-11-29 08:14:09 +0100206 if (!retval) {
207 status = wmi_evaluate_method(EEEPC_WMI_MGMT_GUID, 1,
208 EEEPC_WMI_METHODID_DEVS,
209 &input, NULL);
210 } else {
211 struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER, NULL };
212 union acpi_object *obj;
213 u32 tmp;
214
215 status = wmi_evaluate_method(EEEPC_WMI_MGMT_GUID, 1,
216 EEEPC_WMI_METHODID_DEVS,
217 &input, &output);
218
219 if (ACPI_FAILURE(status))
220 return status;
221
222 obj = (union acpi_object *)output.pointer;
223 if (obj && obj->type == ACPI_TYPE_INTEGER)
224 tmp = (u32)obj->integer.value;
225 else
226 tmp = 0;
227
228 *retval = tmp;
229
230 kfree(obj);
231 }
Yong Wang3d7b1652010-04-11 09:27:54 +0800232
233 return status;
234}
235
Corentin Chary084fca62010-11-29 08:14:06 +0100236/*
237 * LEDs
238 */
239/*
240 * These functions actually update the LED's, and are called from a
241 * workqueue. By doing this as separate work rather than when the LED
242 * subsystem asks, we avoid messing with the Eeepc ACPI stuff during a
243 * potentially bad time, such as a timer interrupt.
244 */
245static void tpd_led_update(struct work_struct *work)
246{
247 int ctrl_param;
248 struct eeepc_wmi *eeepc;
249
250 eeepc = container_of(work, struct eeepc_wmi, tpd_led_work);
251
252 ctrl_param = eeepc->tpd_led_wk;
Corentin Chary8c1b2d82010-11-29 08:14:09 +0100253 eeepc_wmi_set_devstate(EEEPC_WMI_DEVID_TPDLED, ctrl_param, NULL);
Corentin Chary084fca62010-11-29 08:14:06 +0100254}
255
256static void tpd_led_set(struct led_classdev *led_cdev,
257 enum led_brightness value)
258{
259 struct eeepc_wmi *eeepc;
260
261 eeepc = container_of(led_cdev, struct eeepc_wmi, tpd_led);
262
263 eeepc->tpd_led_wk = !!value;
264 queue_work(eeepc->led_workqueue, &eeepc->tpd_led_work);
265}
266
267static int read_tpd_state(struct eeepc_wmi *eeepc)
268{
269 static u32 ctrl_param;
270 acpi_status status;
271
272 status = eeepc_wmi_get_devstate(EEEPC_WMI_DEVID_TPDLED, &ctrl_param);
273
274 if (ACPI_FAILURE(status))
275 return -1;
276 else if (!ctrl_param || ctrl_param == 0x00060000)
277 /*
278 * if touchpad led is present, DSTS will set some bits,
279 * usually 0x00020000.
280 * 0x00060000 means that the device is not supported
281 */
282 return -ENODEV;
283 else
284 /* Status is stored in the first bit */
285 return ctrl_param & 0x1;
286}
287
288static enum led_brightness tpd_led_get(struct led_classdev *led_cdev)
289{
290 struct eeepc_wmi *eeepc;
291
292 eeepc = container_of(led_cdev, struct eeepc_wmi, tpd_led);
293
294 return read_tpd_state(eeepc);
295}
296
297static int eeepc_wmi_led_init(struct eeepc_wmi *eeepc)
298{
299 int rv;
300
301 if (read_tpd_state(eeepc) < 0)
302 return 0;
303
304 eeepc->led_workqueue = create_singlethread_workqueue("led_workqueue");
305 if (!eeepc->led_workqueue)
306 return -ENOMEM;
307 INIT_WORK(&eeepc->tpd_led_work, tpd_led_update);
308
309 eeepc->tpd_led.name = "eeepc::touchpad";
310 eeepc->tpd_led.brightness_set = tpd_led_set;
311 eeepc->tpd_led.brightness_get = tpd_led_get;
312 eeepc->tpd_led.max_brightness = 1;
313
314 rv = led_classdev_register(&eeepc->platform_device->dev,
315 &eeepc->tpd_led);
316 if (rv) {
317 destroy_workqueue(eeepc->led_workqueue);
318 return rv;
319 }
320
321 return 0;
322}
323
324static void eeepc_wmi_led_exit(struct eeepc_wmi *eeepc)
325{
326 if (eeepc->tpd_led.dev)
327 led_classdev_unregister(&eeepc->tpd_led);
328 if (eeepc->led_workqueue)
329 destroy_workqueue(eeepc->led_workqueue);
330}
331
332/*
Corentin Charyba48fdb2010-11-29 08:14:07 +0100333 * Rfkill devices
334 */
335static int eeepc_rfkill_set(void *data, bool blocked)
336{
337 int dev_id = (unsigned long)data;
338 u32 ctrl_param = !blocked;
339
Corentin Chary8c1b2d82010-11-29 08:14:09 +0100340 return eeepc_wmi_set_devstate(dev_id, ctrl_param, NULL);
Corentin Charyba48fdb2010-11-29 08:14:07 +0100341}
342
343static void eeepc_rfkill_query(struct rfkill *rfkill, void *data)
344{
345 int dev_id = (unsigned long)data;
346 u32 ctrl_param;
347 acpi_status status;
348
349 status = eeepc_wmi_get_devstate(dev_id, &ctrl_param);
350
351 if (ACPI_FAILURE(status))
352 return ;
353
354 rfkill_set_sw_state(rfkill, !(ctrl_param & 0x1));
355}
356
357static const struct rfkill_ops eeepc_rfkill_ops = {
358 .set_block = eeepc_rfkill_set,
359 .query = eeepc_rfkill_query,
360};
361
362static int eeepc_new_rfkill(struct eeepc_wmi *eeepc,
363 struct rfkill **rfkill,
364 const char *name,
365 enum rfkill_type type, int dev_id)
366{
367 int result;
368 u32 ctrl_param;
369 acpi_status status;
370
371 status = eeepc_wmi_get_devstate(dev_id, &ctrl_param);
372
373 if (ACPI_FAILURE(status))
374 return -1;
375
376 /* If the device is present, DSTS will always set some bits
377 * 0x00070000 - 1110000000000000000 - device supported
378 * 0x00060000 - 1100000000000000000 - not supported
379 * 0x00020000 - 0100000000000000000 - device supported
380 * 0x00010000 - 0010000000000000000 - not supported / special mode ?
381 */
382 if (!ctrl_param || ctrl_param == 0x00060000)
383 return -ENODEV;
384
385 *rfkill = rfkill_alloc(name, &eeepc->platform_device->dev, type,
386 &eeepc_rfkill_ops, (void *)(long)dev_id);
387
388 if (!*rfkill)
389 return -EINVAL;
390
391 rfkill_init_sw_state(*rfkill, !(ctrl_param & 0x1));
392 result = rfkill_register(*rfkill);
393 if (result) {
394 rfkill_destroy(*rfkill);
395 *rfkill = NULL;
396 return result;
397 }
398 return 0;
399}
400
401static void eeepc_wmi_rfkill_exit(struct eeepc_wmi *eeepc)
402{
403 if (eeepc->wlan_rfkill) {
404 rfkill_unregister(eeepc->wlan_rfkill);
405 rfkill_destroy(eeepc->wlan_rfkill);
406 eeepc->wlan_rfkill = NULL;
407 }
408 if (eeepc->bluetooth_rfkill) {
409 rfkill_unregister(eeepc->bluetooth_rfkill);
410 rfkill_destroy(eeepc->bluetooth_rfkill);
411 eeepc->bluetooth_rfkill = NULL;
412 }
413 if (eeepc->wwan3g_rfkill) {
414 rfkill_unregister(eeepc->wwan3g_rfkill);
415 rfkill_destroy(eeepc->wwan3g_rfkill);
416 eeepc->wwan3g_rfkill = NULL;
417 }
418}
419
420static int eeepc_wmi_rfkill_init(struct eeepc_wmi *eeepc)
421{
422 int result = 0;
423
424 result = eeepc_new_rfkill(eeepc, &eeepc->wlan_rfkill,
425 "eeepc-wlan", RFKILL_TYPE_WLAN,
426 EEEPC_WMI_DEVID_WLAN);
427
428 if (result && result != -ENODEV)
429 goto exit;
430
431 result = eeepc_new_rfkill(eeepc, &eeepc->bluetooth_rfkill,
432 "eeepc-bluetooth", RFKILL_TYPE_BLUETOOTH,
433 EEEPC_WMI_DEVID_BLUETOOTH);
434
435 if (result && result != -ENODEV)
436 goto exit;
437
438 result = eeepc_new_rfkill(eeepc, &eeepc->wwan3g_rfkill,
439 "eeepc-wwan3g", RFKILL_TYPE_WWAN,
440 EEEPC_WMI_DEVID_WWAN3G);
441
442 if (result && result != -ENODEV)
443 goto exit;
444
445exit:
446 if (result && result != -ENODEV)
447 eeepc_wmi_rfkill_exit(eeepc);
448
449 if (result == -ENODEV)
450 result = 0;
451
452 return result;
453}
454
455/*
Corentin Chary084fca62010-11-29 08:14:06 +0100456 * Backlight
457 */
Yong Wang3d7b1652010-04-11 09:27:54 +0800458static int read_brightness(struct backlight_device *bd)
459{
460 static u32 ctrl_param;
461 acpi_status status;
462
463 status = eeepc_wmi_get_devstate(EEEPC_WMI_DEVID_BACKLIGHT, &ctrl_param);
464
465 if (ACPI_FAILURE(status))
466 return -1;
467 else
468 return ctrl_param & 0xFF;
469}
470
471static int update_bl_status(struct backlight_device *bd)
472{
473
474 static u32 ctrl_param;
475 acpi_status status;
476
477 ctrl_param = bd->props.brightness;
478
Corentin Chary8c1b2d82010-11-29 08:14:09 +0100479 status = eeepc_wmi_set_devstate(EEEPC_WMI_DEVID_BACKLIGHT,
480 ctrl_param, NULL);
Yong Wang3d7b1652010-04-11 09:27:54 +0800481
482 if (ACPI_FAILURE(status))
483 return -1;
484 else
485 return 0;
486}
487
488static const struct backlight_ops eeepc_wmi_bl_ops = {
489 .get_brightness = read_brightness,
490 .update_status = update_bl_status,
491};
492
493static int eeepc_wmi_backlight_notify(struct eeepc_wmi *eeepc, int code)
494{
495 struct backlight_device *bd = eeepc->backlight_device;
496 int old = bd->props.brightness;
Daniel Mackb7670ed2010-05-19 12:37:01 +0200497 int new = old;
Yong Wang3d7b1652010-04-11 09:27:54 +0800498
499 if (code >= NOTIFY_BRNUP_MIN && code <= NOTIFY_BRNUP_MAX)
500 new = code - NOTIFY_BRNUP_MIN + 1;
501 else if (code >= NOTIFY_BRNDOWN_MIN && code <= NOTIFY_BRNDOWN_MAX)
502 new = code - NOTIFY_BRNDOWN_MIN;
503
504 bd->props.brightness = new;
505 backlight_update_status(bd);
506 backlight_force_update(bd, BACKLIGHT_UPDATE_HOTKEY);
507
508 return old;
509}
510
511static int eeepc_wmi_backlight_init(struct eeepc_wmi *eeepc)
512{
513 struct backlight_device *bd;
514 struct backlight_properties props;
515
516 memset(&props, 0, sizeof(struct backlight_properties));
517 props.max_brightness = 15;
518 bd = backlight_device_register(EEEPC_WMI_FILE,
Corentin Chary27c136c2010-11-29 08:14:05 +0100519 &eeepc->platform_device->dev, eeepc,
Yong Wang3d7b1652010-04-11 09:27:54 +0800520 &eeepc_wmi_bl_ops, &props);
521 if (IS_ERR(bd)) {
522 pr_err("Could not register backlight device\n");
523 return PTR_ERR(bd);
524 }
525
526 eeepc->backlight_device = bd;
527
528 bd->props.brightness = read_brightness(bd);
529 bd->props.power = FB_BLANK_UNBLANK;
530 backlight_update_status(bd);
531
532 return 0;
533}
534
535static void eeepc_wmi_backlight_exit(struct eeepc_wmi *eeepc)
536{
537 if (eeepc->backlight_device)
538 backlight_device_unregister(eeepc->backlight_device);
539
540 eeepc->backlight_device = NULL;
541}
542
543static void eeepc_wmi_notify(u32 value, void *context)
544{
545 struct eeepc_wmi *eeepc = context;
546 struct acpi_buffer response = { ACPI_ALLOCATE_BUFFER, NULL };
547 union acpi_object *obj;
548 acpi_status status;
549 int code;
550 int orig_code;
551
552 status = wmi_get_event_data(value, &response);
553 if (status != AE_OK) {
554 pr_err("bad event status 0x%x\n", status);
555 return;
556 }
557
558 obj = (union acpi_object *)response.pointer;
559
560 if (obj && obj->type == ACPI_TYPE_INTEGER) {
561 code = obj->integer.value;
562 orig_code = code;
563
564 if (code >= NOTIFY_BRNUP_MIN && code <= NOTIFY_BRNUP_MAX)
565 code = NOTIFY_BRNUP_MIN;
566 else if (code >= NOTIFY_BRNDOWN_MIN &&
567 code <= NOTIFY_BRNDOWN_MAX)
568 code = NOTIFY_BRNDOWN_MIN;
569
570 if (code == NOTIFY_BRNUP_MIN || code == NOTIFY_BRNDOWN_MIN) {
571 if (!acpi_video_backlight_support())
572 eeepc_wmi_backlight_notify(eeepc, orig_code);
573 }
574
575 if (!sparse_keymap_report_event(eeepc->inputdev,
576 code, 1, true))
577 pr_info("Unknown key %x pressed\n", code);
578 }
579
580 kfree(obj);
581}
582
Dmitry Torokhov67fa38e2010-11-03 11:14:01 -0700583static ssize_t store_cpufv(struct device *dev, struct device_attribute *attr,
584 const char *buf, size_t count)
Chris Bagwell7f80d732010-10-11 18:47:18 -0500585{
586 int value;
587 struct acpi_buffer input = { (acpi_size)sizeof(value), &value };
588 acpi_status status;
589
590 if (!count || sscanf(buf, "%i", &value) != 1)
591 return -EINVAL;
592 if (value < 0 || value > 2)
593 return -EINVAL;
594
595 status = wmi_evaluate_method(EEEPC_WMI_MGMT_GUID,
596 1, EEEPC_WMI_METHODID_CFVS, &input, NULL);
597
598 if (ACPI_FAILURE(status))
599 return -EIO;
600 else
601 return count;
602}
603
604static DEVICE_ATTR(cpufv, S_IRUGO | S_IWUSR, NULL, store_cpufv);
605
Corentin Chary4e37b422010-11-29 08:14:08 +0100606static struct attribute *platform_attributes[] = {
607 &dev_attr_cpufv.attr,
608 NULL
609};
610
611static struct attribute_group platform_attribute_group = {
612 .attrs = platform_attributes
613};
614
Chris Bagwell7f80d732010-10-11 18:47:18 -0500615static void eeepc_wmi_sysfs_exit(struct platform_device *device)
616{
Corentin Chary4e37b422010-11-29 08:14:08 +0100617 sysfs_remove_group(&device->dev.kobj, &platform_attribute_group);
Chris Bagwell7f80d732010-10-11 18:47:18 -0500618}
619
620static int eeepc_wmi_sysfs_init(struct platform_device *device)
621{
Corentin Chary4e37b422010-11-29 08:14:08 +0100622 return sysfs_create_group(&device->dev.kobj, &platform_attribute_group);
Chris Bagwell7f80d732010-10-11 18:47:18 -0500623}
624
Corentin Chary27c136c2010-11-29 08:14:05 +0100625/*
626 * Platform device
627 */
628static int __init eeepc_wmi_platform_init(struct eeepc_wmi *eeepc)
629{
630 int err;
631
632 eeepc->platform_device = platform_device_alloc(EEEPC_WMI_FILE, -1);
633 if (!eeepc->platform_device)
634 return -ENOMEM;
635 platform_set_drvdata(eeepc->platform_device, eeepc);
636
637 err = platform_device_add(eeepc->platform_device);
638 if (err)
639 goto fail_platform_device;
640
641 err = eeepc_wmi_sysfs_init(eeepc->platform_device);
642 if (err)
643 goto fail_sysfs;
644 return 0;
645
646fail_sysfs:
647 platform_device_del(eeepc->platform_device);
648fail_platform_device:
649 platform_device_put(eeepc->platform_device);
650 return err;
651}
652
653static void eeepc_wmi_platform_exit(struct eeepc_wmi *eeepc)
654{
655 eeepc_wmi_sysfs_exit(eeepc->platform_device);
656 platform_device_unregister(eeepc->platform_device);
657}
658
659/*
Corentin Chary8c1b2d82010-11-29 08:14:09 +0100660 * debugfs
661 */
662struct eeepc_wmi_debugfs_node {
663 struct eeepc_wmi *eeepc;
664 char *name;
665 int (*show)(struct seq_file *m, void *data);
666};
667
668static int show_dsts(struct seq_file *m, void *data)
669{
670 struct eeepc_wmi *eeepc = m->private;
671 acpi_status status;
672 u32 retval = -1;
673
674 status = eeepc_wmi_get_devstate(eeepc->debug.dev_id, &retval);
675
676 if (ACPI_FAILURE(status))
677 return -EIO;
678
679 seq_printf(m, "DSTS(%x) = %x\n", eeepc->debug.dev_id, retval);
680
681 return 0;
682}
683
684static int show_devs(struct seq_file *m, void *data)
685{
686 struct eeepc_wmi *eeepc = m->private;
687 acpi_status status;
688 u32 retval = -1;
689
690 status = eeepc_wmi_set_devstate(eeepc->debug.dev_id,
691 eeepc->debug.ctrl_param, &retval);
692 if (ACPI_FAILURE(status))
693 return -EIO;
694
695 seq_printf(m, "DEVS(%x, %x) = %x\n", eeepc->debug.dev_id,
696 eeepc->debug.ctrl_param, retval);
697
698 return 0;
699}
700
701static struct eeepc_wmi_debugfs_node eeepc_wmi_debug_files[] = {
702 { NULL, "devs", show_devs },
703 { NULL, "dsts", show_dsts },
704};
705
706static int eeepc_wmi_debugfs_open(struct inode *inode, struct file *file)
707{
708 struct eeepc_wmi_debugfs_node *node = inode->i_private;
709
710 return single_open(file, node->show, node->eeepc);
711}
712
713static const struct file_operations eeepc_wmi_debugfs_io_ops = {
714 .owner = THIS_MODULE,
715 .open = eeepc_wmi_debugfs_open,
716 .read = seq_read,
717 .llseek = seq_lseek,
718 .release = single_release,
719};
720
721static void eeepc_wmi_debugfs_exit(struct eeepc_wmi *eeepc)
722{
723 debugfs_remove_recursive(eeepc->debug.root);
724}
725
726static int eeepc_wmi_debugfs_init(struct eeepc_wmi *eeepc)
727{
728 struct dentry *dent;
729 int i;
730
731 eeepc->debug.root = debugfs_create_dir(EEEPC_WMI_FILE, NULL);
732 if (!eeepc->debug.root) {
733 pr_err("failed to create debugfs directory");
734 goto error_debugfs;
735 }
736
737 dent = debugfs_create_x32("dev_id", S_IRUGO|S_IWUSR,
738 eeepc->debug.root, &eeepc->debug.dev_id);
739 if (!dent)
740 goto error_debugfs;
741
742 dent = debugfs_create_x32("ctrl_param", S_IRUGO|S_IWUSR,
743 eeepc->debug.root, &eeepc->debug.ctrl_param);
744 if (!dent)
745 goto error_debugfs;
746
747 for (i = 0; i < ARRAY_SIZE(eeepc_wmi_debug_files); i++) {
748 struct eeepc_wmi_debugfs_node *node = &eeepc_wmi_debug_files[i];
749
750 node->eeepc = eeepc;
751 dent = debugfs_create_file(node->name, S_IFREG | S_IRUGO,
752 eeepc->debug.root, node,
753 &eeepc_wmi_debugfs_io_ops);
754 if (!dent) {
755 pr_err("failed to create debug file: %s\n", node->name);
756 goto error_debugfs;
757 }
758 }
759
760 return 0;
761
762error_debugfs:
763 eeepc_wmi_debugfs_exit(eeepc);
764 return -ENOMEM;
765}
766
767/*
Corentin Chary27c136c2010-11-29 08:14:05 +0100768 * WMI Driver
769 */
770static struct platform_device * __init eeepc_wmi_add(void)
Yong Wangee027e42010-03-21 10:26:34 +0800771{
Yong Wang45f2c692010-04-11 09:27:19 +0800772 struct eeepc_wmi *eeepc;
Yong Wangee027e42010-03-21 10:26:34 +0800773 acpi_status status;
Corentin Chary27c136c2010-11-29 08:14:05 +0100774 int err;
Yong Wangee027e42010-03-21 10:26:34 +0800775
Corentin Chary27c136c2010-11-29 08:14:05 +0100776 eeepc = kzalloc(sizeof(struct eeepc_wmi), GFP_KERNEL);
777 if (!eeepc)
778 return ERR_PTR(-ENOMEM);
779
780 /*
781 * Register the platform device first. It is used as a parent for the
782 * sub-devices below.
783 */
784 err = eeepc_wmi_platform_init(eeepc);
785 if (err)
786 goto fail_platform;
Yong Wang45f2c692010-04-11 09:27:19 +0800787
788 err = eeepc_wmi_input_init(eeepc);
789 if (err)
Corentin Chary27c136c2010-11-29 08:14:05 +0100790 goto fail_input;
Yong Wang3d7b1652010-04-11 09:27:54 +0800791
Corentin Chary084fca62010-11-29 08:14:06 +0100792 err = eeepc_wmi_led_init(eeepc);
793 if (err)
794 goto fail_leds;
795
Corentin Charyba48fdb2010-11-29 08:14:07 +0100796 err = eeepc_wmi_rfkill_init(eeepc);
797 if (err)
798 goto fail_rfkill;
799
Yong Wang3d7b1652010-04-11 09:27:54 +0800800 if (!acpi_video_backlight_support()) {
801 err = eeepc_wmi_backlight_init(eeepc);
802 if (err)
Corentin Chary27c136c2010-11-29 08:14:05 +0100803 goto fail_backlight;
Yong Wang3d7b1652010-04-11 09:27:54 +0800804 } else
805 pr_info("Backlight controlled by ACPI video driver\n");
Yong Wang45f2c692010-04-11 09:27:19 +0800806
807 status = wmi_install_notify_handler(EEEPC_WMI_EVENT_GUID,
Corentin Chary27c136c2010-11-29 08:14:05 +0100808 eeepc_wmi_notify, eeepc);
Yong Wang45f2c692010-04-11 09:27:19 +0800809 if (ACPI_FAILURE(status)) {
810 pr_err("Unable to register notify handler - %d\n",
811 status);
812 err = -ENODEV;
Corentin Chary27c136c2010-11-29 08:14:05 +0100813 goto fail_wmi_handler;
Yong Wang45f2c692010-04-11 09:27:19 +0800814 }
815
Corentin Chary8c1b2d82010-11-29 08:14:09 +0100816 err = eeepc_wmi_debugfs_init(eeepc);
817 if (err)
818 goto fail_debugfs;
819
Corentin Chary27c136c2010-11-29 08:14:05 +0100820 return eeepc->platform_device;
Yong Wang45f2c692010-04-11 09:27:19 +0800821
Corentin Chary8c1b2d82010-11-29 08:14:09 +0100822fail_debugfs:
823 wmi_remove_notify_handler(EEEPC_WMI_EVENT_GUID);
Corentin Chary27c136c2010-11-29 08:14:05 +0100824fail_wmi_handler:
Yong Wang3d7b1652010-04-11 09:27:54 +0800825 eeepc_wmi_backlight_exit(eeepc);
Corentin Chary27c136c2010-11-29 08:14:05 +0100826fail_backlight:
Corentin Charyba48fdb2010-11-29 08:14:07 +0100827 eeepc_wmi_rfkill_exit(eeepc);
828fail_rfkill:
Corentin Chary084fca62010-11-29 08:14:06 +0100829 eeepc_wmi_led_exit(eeepc);
830fail_leds:
Yong Wang45f2c692010-04-11 09:27:19 +0800831 eeepc_wmi_input_exit(eeepc);
Corentin Chary27c136c2010-11-29 08:14:05 +0100832fail_input:
833 eeepc_wmi_platform_exit(eeepc);
834fail_platform:
835 kfree(eeepc);
836 return ERR_PTR(err);
Yong Wang45f2c692010-04-11 09:27:19 +0800837}
838
Corentin Chary27c136c2010-11-29 08:14:05 +0100839static int eeepc_wmi_remove(struct platform_device *device)
Yong Wang45f2c692010-04-11 09:27:19 +0800840{
841 struct eeepc_wmi *eeepc;
842
843 eeepc = platform_get_drvdata(device);
844 wmi_remove_notify_handler(EEEPC_WMI_EVENT_GUID);
Yong Wang3d7b1652010-04-11 09:27:54 +0800845 eeepc_wmi_backlight_exit(eeepc);
Yong Wang45f2c692010-04-11 09:27:19 +0800846 eeepc_wmi_input_exit(eeepc);
Corentin Chary084fca62010-11-29 08:14:06 +0100847 eeepc_wmi_led_exit(eeepc);
Corentin Charyba48fdb2010-11-29 08:14:07 +0100848 eeepc_wmi_rfkill_exit(eeepc);
Corentin Chary8c1b2d82010-11-29 08:14:09 +0100849 eeepc_wmi_debugfs_exit(eeepc);
Corentin Chary27c136c2010-11-29 08:14:05 +0100850 eeepc_wmi_platform_exit(eeepc);
Yong Wang45f2c692010-04-11 09:27:19 +0800851
Corentin Chary27c136c2010-11-29 08:14:05 +0100852 kfree(eeepc);
Yong Wang45f2c692010-04-11 09:27:19 +0800853 return 0;
854}
855
856static struct platform_driver platform_driver = {
857 .driver = {
858 .name = EEEPC_WMI_FILE,
859 .owner = THIS_MODULE,
860 },
Yong Wang45f2c692010-04-11 09:27:19 +0800861};
862
863static int __init eeepc_wmi_init(void)
864{
Yong Wang45f2c692010-04-11 09:27:19 +0800865 int err;
866
Yong Wang3d7b1652010-04-11 09:27:54 +0800867 if (!wmi_has_guid(EEEPC_WMI_EVENT_GUID) ||
868 !wmi_has_guid(EEEPC_WMI_MGMT_GUID)) {
Yong Wang81248882010-04-11 09:26:33 +0800869 pr_warning("No known WMI GUID found\n");
Yong Wangee027e42010-03-21 10:26:34 +0800870 return -ENODEV;
871 }
872
Corentin Chary27c136c2010-11-29 08:14:05 +0100873 platform_device = eeepc_wmi_add();
874 if (IS_ERR(platform_device)) {
875 err = PTR_ERR(platform_device);
876 goto fail_eeepc_wmi;
Yong Wang81248882010-04-11 09:26:33 +0800877 }
Yong Wangee027e42010-03-21 10:26:34 +0800878
Yong Wang45f2c692010-04-11 09:27:19 +0800879 err = platform_driver_register(&platform_driver);
880 if (err) {
881 pr_warning("Unable to register platform driver\n");
Corentin Chary27c136c2010-11-29 08:14:05 +0100882 goto fail_platform_driver;
Yong Wangee027e42010-03-21 10:26:34 +0800883 }
884
885 return 0;
Yong Wang45f2c692010-04-11 09:27:19 +0800886
Corentin Chary27c136c2010-11-29 08:14:05 +0100887fail_platform_driver:
888 eeepc_wmi_remove(platform_device);
889fail_eeepc_wmi:
Yong Wang45f2c692010-04-11 09:27:19 +0800890 return err;
Yong Wangee027e42010-03-21 10:26:34 +0800891}
892
893static void __exit eeepc_wmi_exit(void)
894{
Corentin Chary27c136c2010-11-29 08:14:05 +0100895 eeepc_wmi_remove(platform_device);
Yong Wang45f2c692010-04-11 09:27:19 +0800896 platform_driver_unregister(&platform_driver);
Yong Wangee027e42010-03-21 10:26:34 +0800897}
898
899module_init(eeepc_wmi_init);
900module_exit(eeepc_wmi_exit);