blob: fb548f4b03e0aba8d6f2f4b1079e8538646b5728 [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>
Yong Wang45f2c692010-04-11 09:27:19 +080039#include <linux/platform_device.h>
Yong Wangee027e42010-03-21 10:26:34 +080040#include <acpi/acpi_bus.h>
41#include <acpi/acpi_drivers.h>
42
Yong Wang45f2c692010-04-11 09:27:19 +080043#define EEEPC_WMI_FILE "eeepc-wmi"
44
Yong Wangee027e42010-03-21 10:26:34 +080045MODULE_AUTHOR("Yong Wang <yong.y.wang@intel.com>");
46MODULE_DESCRIPTION("Eee PC WMI Hotkey Driver");
47MODULE_LICENSE("GPL");
48
49#define EEEPC_WMI_EVENT_GUID "ABBC0F72-8EA1-11D1-00A0-C90629100000"
Yong Wang3d7b1652010-04-11 09:27:54 +080050#define EEEPC_WMI_MGMT_GUID "97845ED0-4E6D-11DE-8A39-0800200C9A66"
Yong Wangee027e42010-03-21 10:26:34 +080051
52MODULE_ALIAS("wmi:"EEEPC_WMI_EVENT_GUID);
Yong Wang3d7b1652010-04-11 09:27:54 +080053MODULE_ALIAS("wmi:"EEEPC_WMI_MGMT_GUID);
Yong Wangee027e42010-03-21 10:26:34 +080054
55#define NOTIFY_BRNUP_MIN 0x11
56#define NOTIFY_BRNUP_MAX 0x1f
57#define NOTIFY_BRNDOWN_MIN 0x20
58#define NOTIFY_BRNDOWN_MAX 0x2e
59
Yong Wang3d7b1652010-04-11 09:27:54 +080060#define EEEPC_WMI_METHODID_DEVS 0x53564544
61#define EEEPC_WMI_METHODID_DSTS 0x53544344
Chris Bagwell7f80d732010-10-11 18:47:18 -050062#define EEEPC_WMI_METHODID_CFVS 0x53564643
Yong Wang3d7b1652010-04-11 09:27:54 +080063
64#define EEEPC_WMI_DEVID_BACKLIGHT 0x00050012
Corentin Chary084fca62010-11-29 08:14:06 +010065#define EEEPC_WMI_DEVID_TPDLED 0x00100011
Corentin Charyba48fdb2010-11-29 08:14:07 +010066#define EEEPC_WMI_DEVID_WLAN 0x00010011
67#define EEEPC_WMI_DEVID_BLUETOOTH 0x00010013
68#define EEEPC_WMI_DEVID_WWAN3G 0x00010019
Yong Wang3d7b1652010-04-11 09:27:54 +080069
Yong Wangee027e42010-03-21 10:26:34 +080070static const struct key_entry eeepc_wmi_keymap[] = {
71 /* Sleep already handled via generic ACPI code */
72 { KE_KEY, 0x5d, { KEY_WLAN } },
73 { KE_KEY, 0x32, { KEY_MUTE } },
74 { KE_KEY, 0x31, { KEY_VOLUMEDOWN } },
75 { KE_KEY, 0x30, { KEY_VOLUMEUP } },
76 { KE_IGNORE, NOTIFY_BRNDOWN_MIN, { KEY_BRIGHTNESSDOWN } },
77 { KE_IGNORE, NOTIFY_BRNUP_MIN, { KEY_BRIGHTNESSUP } },
78 { KE_KEY, 0xcc, { KEY_SWITCHVIDEOMODE } },
Chris Bagwelleda17482010-10-11 18:47:17 -050079 { KE_KEY, 0x6b, { KEY_F13 } }, /* Disable Touchpad */
80 { KE_KEY, 0xe1, { KEY_F14 } },
81 { KE_KEY, 0xe9, { KEY_DISPLAY_OFF } },
82 { KE_KEY, 0xe0, { KEY_PROG1 } },
83 { KE_KEY, 0x5c, { KEY_F15 } },
Yong Wangee027e42010-03-21 10:26:34 +080084 { KE_END, 0},
85};
86
Yong Wang3d7b1652010-04-11 09:27:54 +080087struct bios_args {
88 u32 dev_id;
89 u32 ctrl_param;
90};
91
Yong Wang81248882010-04-11 09:26:33 +080092struct eeepc_wmi {
93 struct input_dev *inputdev;
Yong Wang3d7b1652010-04-11 09:27:54 +080094 struct backlight_device *backlight_device;
Corentin Chary27c136c2010-11-29 08:14:05 +010095 struct platform_device *platform_device;
Corentin Chary084fca62010-11-29 08:14:06 +010096
97 struct led_classdev tpd_led;
98 int tpd_led_wk;
99 struct workqueue_struct *led_workqueue;
100 struct work_struct tpd_led_work;
Corentin Charyba48fdb2010-11-29 08:14:07 +0100101
102 struct rfkill *wlan_rfkill;
103 struct rfkill *bluetooth_rfkill;
104 struct rfkill *wwan3g_rfkill;
Yong Wang81248882010-04-11 09:26:33 +0800105};
106
Corentin Chary27c136c2010-11-29 08:14:05 +0100107/* Only used in eeepc_wmi_init() and eeepc_wmi_exit() */
Yong Wang45f2c692010-04-11 09:27:19 +0800108static struct platform_device *platform_device;
Yong Wangee027e42010-03-21 10:26:34 +0800109
Yong Wang81248882010-04-11 09:26:33 +0800110static int eeepc_wmi_input_init(struct eeepc_wmi *eeepc)
Yong Wangee027e42010-03-21 10:26:34 +0800111{
112 int err;
113
Yong Wang81248882010-04-11 09:26:33 +0800114 eeepc->inputdev = input_allocate_device();
115 if (!eeepc->inputdev)
Yong Wangee027e42010-03-21 10:26:34 +0800116 return -ENOMEM;
117
Yong Wang81248882010-04-11 09:26:33 +0800118 eeepc->inputdev->name = "Eee PC WMI hotkeys";
Yong Wang45f2c692010-04-11 09:27:19 +0800119 eeepc->inputdev->phys = EEEPC_WMI_FILE "/input0";
Yong Wang81248882010-04-11 09:26:33 +0800120 eeepc->inputdev->id.bustype = BUS_HOST;
Corentin Chary27c136c2010-11-29 08:14:05 +0100121 eeepc->inputdev->dev.parent = &eeepc->platform_device->dev;
Yong Wangee027e42010-03-21 10:26:34 +0800122
Yong Wang81248882010-04-11 09:26:33 +0800123 err = sparse_keymap_setup(eeepc->inputdev, eeepc_wmi_keymap, NULL);
Yong Wangee027e42010-03-21 10:26:34 +0800124 if (err)
125 goto err_free_dev;
126
Yong Wang81248882010-04-11 09:26:33 +0800127 err = input_register_device(eeepc->inputdev);
Yong Wangee027e42010-03-21 10:26:34 +0800128 if (err)
129 goto err_free_keymap;
130
131 return 0;
132
133err_free_keymap:
Yong Wang81248882010-04-11 09:26:33 +0800134 sparse_keymap_free(eeepc->inputdev);
Yong Wangee027e42010-03-21 10:26:34 +0800135err_free_dev:
Yong Wang81248882010-04-11 09:26:33 +0800136 input_free_device(eeepc->inputdev);
Yong Wangee027e42010-03-21 10:26:34 +0800137 return err;
138}
139
Yong Wang81248882010-04-11 09:26:33 +0800140static void eeepc_wmi_input_exit(struct eeepc_wmi *eeepc)
141{
142 if (eeepc->inputdev) {
143 sparse_keymap_free(eeepc->inputdev);
144 input_unregister_device(eeepc->inputdev);
145 }
146
147 eeepc->inputdev = NULL;
148}
149
Yong Wang3d7b1652010-04-11 09:27:54 +0800150static acpi_status eeepc_wmi_get_devstate(u32 dev_id, u32 *ctrl_param)
151{
152 struct acpi_buffer input = { (acpi_size)sizeof(u32), &dev_id };
153 struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER, NULL };
154 union acpi_object *obj;
155 acpi_status status;
156 u32 tmp;
157
158 status = wmi_evaluate_method(EEEPC_WMI_MGMT_GUID,
159 1, EEEPC_WMI_METHODID_DSTS, &input, &output);
160
161 if (ACPI_FAILURE(status))
162 return status;
163
164 obj = (union acpi_object *)output.pointer;
165 if (obj && obj->type == ACPI_TYPE_INTEGER)
166 tmp = (u32)obj->integer.value;
167 else
168 tmp = 0;
169
170 if (ctrl_param)
171 *ctrl_param = tmp;
172
173 kfree(obj);
174
175 return status;
176
177}
178
179static acpi_status eeepc_wmi_set_devstate(u32 dev_id, u32 ctrl_param)
180{
181 struct bios_args args = {
182 .dev_id = dev_id,
183 .ctrl_param = ctrl_param,
184 };
185 struct acpi_buffer input = { (acpi_size)sizeof(args), &args };
186 acpi_status status;
187
188 status = wmi_evaluate_method(EEEPC_WMI_MGMT_GUID,
189 1, EEEPC_WMI_METHODID_DEVS, &input, NULL);
190
191 return status;
192}
193
Corentin Chary084fca62010-11-29 08:14:06 +0100194/*
195 * LEDs
196 */
197/*
198 * These functions actually update the LED's, and are called from a
199 * workqueue. By doing this as separate work rather than when the LED
200 * subsystem asks, we avoid messing with the Eeepc ACPI stuff during a
201 * potentially bad time, such as a timer interrupt.
202 */
203static void tpd_led_update(struct work_struct *work)
204{
205 int ctrl_param;
206 struct eeepc_wmi *eeepc;
207
208 eeepc = container_of(work, struct eeepc_wmi, tpd_led_work);
209
210 ctrl_param = eeepc->tpd_led_wk;
211 eeepc_wmi_set_devstate(EEEPC_WMI_DEVID_TPDLED, ctrl_param);
212}
213
214static void tpd_led_set(struct led_classdev *led_cdev,
215 enum led_brightness value)
216{
217 struct eeepc_wmi *eeepc;
218
219 eeepc = container_of(led_cdev, struct eeepc_wmi, tpd_led);
220
221 eeepc->tpd_led_wk = !!value;
222 queue_work(eeepc->led_workqueue, &eeepc->tpd_led_work);
223}
224
225static int read_tpd_state(struct eeepc_wmi *eeepc)
226{
227 static u32 ctrl_param;
228 acpi_status status;
229
230 status = eeepc_wmi_get_devstate(EEEPC_WMI_DEVID_TPDLED, &ctrl_param);
231
232 if (ACPI_FAILURE(status))
233 return -1;
234 else if (!ctrl_param || ctrl_param == 0x00060000)
235 /*
236 * if touchpad led is present, DSTS will set some bits,
237 * usually 0x00020000.
238 * 0x00060000 means that the device is not supported
239 */
240 return -ENODEV;
241 else
242 /* Status is stored in the first bit */
243 return ctrl_param & 0x1;
244}
245
246static enum led_brightness tpd_led_get(struct led_classdev *led_cdev)
247{
248 struct eeepc_wmi *eeepc;
249
250 eeepc = container_of(led_cdev, struct eeepc_wmi, tpd_led);
251
252 return read_tpd_state(eeepc);
253}
254
255static int eeepc_wmi_led_init(struct eeepc_wmi *eeepc)
256{
257 int rv;
258
259 if (read_tpd_state(eeepc) < 0)
260 return 0;
261
262 eeepc->led_workqueue = create_singlethread_workqueue("led_workqueue");
263 if (!eeepc->led_workqueue)
264 return -ENOMEM;
265 INIT_WORK(&eeepc->tpd_led_work, tpd_led_update);
266
267 eeepc->tpd_led.name = "eeepc::touchpad";
268 eeepc->tpd_led.brightness_set = tpd_led_set;
269 eeepc->tpd_led.brightness_get = tpd_led_get;
270 eeepc->tpd_led.max_brightness = 1;
271
272 rv = led_classdev_register(&eeepc->platform_device->dev,
273 &eeepc->tpd_led);
274 if (rv) {
275 destroy_workqueue(eeepc->led_workqueue);
276 return rv;
277 }
278
279 return 0;
280}
281
282static void eeepc_wmi_led_exit(struct eeepc_wmi *eeepc)
283{
284 if (eeepc->tpd_led.dev)
285 led_classdev_unregister(&eeepc->tpd_led);
286 if (eeepc->led_workqueue)
287 destroy_workqueue(eeepc->led_workqueue);
288}
289
290/*
Corentin Charyba48fdb2010-11-29 08:14:07 +0100291 * Rfkill devices
292 */
293static int eeepc_rfkill_set(void *data, bool blocked)
294{
295 int dev_id = (unsigned long)data;
296 u32 ctrl_param = !blocked;
297
298 return eeepc_wmi_set_devstate(dev_id, ctrl_param);
299}
300
301static void eeepc_rfkill_query(struct rfkill *rfkill, void *data)
302{
303 int dev_id = (unsigned long)data;
304 u32 ctrl_param;
305 acpi_status status;
306
307 status = eeepc_wmi_get_devstate(dev_id, &ctrl_param);
308
309 if (ACPI_FAILURE(status))
310 return ;
311
312 rfkill_set_sw_state(rfkill, !(ctrl_param & 0x1));
313}
314
315static const struct rfkill_ops eeepc_rfkill_ops = {
316 .set_block = eeepc_rfkill_set,
317 .query = eeepc_rfkill_query,
318};
319
320static int eeepc_new_rfkill(struct eeepc_wmi *eeepc,
321 struct rfkill **rfkill,
322 const char *name,
323 enum rfkill_type type, int dev_id)
324{
325 int result;
326 u32 ctrl_param;
327 acpi_status status;
328
329 status = eeepc_wmi_get_devstate(dev_id, &ctrl_param);
330
331 if (ACPI_FAILURE(status))
332 return -1;
333
334 /* If the device is present, DSTS will always set some bits
335 * 0x00070000 - 1110000000000000000 - device supported
336 * 0x00060000 - 1100000000000000000 - not supported
337 * 0x00020000 - 0100000000000000000 - device supported
338 * 0x00010000 - 0010000000000000000 - not supported / special mode ?
339 */
340 if (!ctrl_param || ctrl_param == 0x00060000)
341 return -ENODEV;
342
343 *rfkill = rfkill_alloc(name, &eeepc->platform_device->dev, type,
344 &eeepc_rfkill_ops, (void *)(long)dev_id);
345
346 if (!*rfkill)
347 return -EINVAL;
348
349 rfkill_init_sw_state(*rfkill, !(ctrl_param & 0x1));
350 result = rfkill_register(*rfkill);
351 if (result) {
352 rfkill_destroy(*rfkill);
353 *rfkill = NULL;
354 return result;
355 }
356 return 0;
357}
358
359static void eeepc_wmi_rfkill_exit(struct eeepc_wmi *eeepc)
360{
361 if (eeepc->wlan_rfkill) {
362 rfkill_unregister(eeepc->wlan_rfkill);
363 rfkill_destroy(eeepc->wlan_rfkill);
364 eeepc->wlan_rfkill = NULL;
365 }
366 if (eeepc->bluetooth_rfkill) {
367 rfkill_unregister(eeepc->bluetooth_rfkill);
368 rfkill_destroy(eeepc->bluetooth_rfkill);
369 eeepc->bluetooth_rfkill = NULL;
370 }
371 if (eeepc->wwan3g_rfkill) {
372 rfkill_unregister(eeepc->wwan3g_rfkill);
373 rfkill_destroy(eeepc->wwan3g_rfkill);
374 eeepc->wwan3g_rfkill = NULL;
375 }
376}
377
378static int eeepc_wmi_rfkill_init(struct eeepc_wmi *eeepc)
379{
380 int result = 0;
381
382 result = eeepc_new_rfkill(eeepc, &eeepc->wlan_rfkill,
383 "eeepc-wlan", RFKILL_TYPE_WLAN,
384 EEEPC_WMI_DEVID_WLAN);
385
386 if (result && result != -ENODEV)
387 goto exit;
388
389 result = eeepc_new_rfkill(eeepc, &eeepc->bluetooth_rfkill,
390 "eeepc-bluetooth", RFKILL_TYPE_BLUETOOTH,
391 EEEPC_WMI_DEVID_BLUETOOTH);
392
393 if (result && result != -ENODEV)
394 goto exit;
395
396 result = eeepc_new_rfkill(eeepc, &eeepc->wwan3g_rfkill,
397 "eeepc-wwan3g", RFKILL_TYPE_WWAN,
398 EEEPC_WMI_DEVID_WWAN3G);
399
400 if (result && result != -ENODEV)
401 goto exit;
402
403exit:
404 if (result && result != -ENODEV)
405 eeepc_wmi_rfkill_exit(eeepc);
406
407 if (result == -ENODEV)
408 result = 0;
409
410 return result;
411}
412
413/*
Corentin Chary084fca62010-11-29 08:14:06 +0100414 * Backlight
415 */
Yong Wang3d7b1652010-04-11 09:27:54 +0800416static int read_brightness(struct backlight_device *bd)
417{
418 static u32 ctrl_param;
419 acpi_status status;
420
421 status = eeepc_wmi_get_devstate(EEEPC_WMI_DEVID_BACKLIGHT, &ctrl_param);
422
423 if (ACPI_FAILURE(status))
424 return -1;
425 else
426 return ctrl_param & 0xFF;
427}
428
429static int update_bl_status(struct backlight_device *bd)
430{
431
432 static u32 ctrl_param;
433 acpi_status status;
434
435 ctrl_param = bd->props.brightness;
436
437 status = eeepc_wmi_set_devstate(EEEPC_WMI_DEVID_BACKLIGHT, ctrl_param);
438
439 if (ACPI_FAILURE(status))
440 return -1;
441 else
442 return 0;
443}
444
445static const struct backlight_ops eeepc_wmi_bl_ops = {
446 .get_brightness = read_brightness,
447 .update_status = update_bl_status,
448};
449
450static int eeepc_wmi_backlight_notify(struct eeepc_wmi *eeepc, int code)
451{
452 struct backlight_device *bd = eeepc->backlight_device;
453 int old = bd->props.brightness;
Daniel Mackb7670ed2010-05-19 12:37:01 +0200454 int new = old;
Yong Wang3d7b1652010-04-11 09:27:54 +0800455
456 if (code >= NOTIFY_BRNUP_MIN && code <= NOTIFY_BRNUP_MAX)
457 new = code - NOTIFY_BRNUP_MIN + 1;
458 else if (code >= NOTIFY_BRNDOWN_MIN && code <= NOTIFY_BRNDOWN_MAX)
459 new = code - NOTIFY_BRNDOWN_MIN;
460
461 bd->props.brightness = new;
462 backlight_update_status(bd);
463 backlight_force_update(bd, BACKLIGHT_UPDATE_HOTKEY);
464
465 return old;
466}
467
468static int eeepc_wmi_backlight_init(struct eeepc_wmi *eeepc)
469{
470 struct backlight_device *bd;
471 struct backlight_properties props;
472
473 memset(&props, 0, sizeof(struct backlight_properties));
474 props.max_brightness = 15;
475 bd = backlight_device_register(EEEPC_WMI_FILE,
Corentin Chary27c136c2010-11-29 08:14:05 +0100476 &eeepc->platform_device->dev, eeepc,
Yong Wang3d7b1652010-04-11 09:27:54 +0800477 &eeepc_wmi_bl_ops, &props);
478 if (IS_ERR(bd)) {
479 pr_err("Could not register backlight device\n");
480 return PTR_ERR(bd);
481 }
482
483 eeepc->backlight_device = bd;
484
485 bd->props.brightness = read_brightness(bd);
486 bd->props.power = FB_BLANK_UNBLANK;
487 backlight_update_status(bd);
488
489 return 0;
490}
491
492static void eeepc_wmi_backlight_exit(struct eeepc_wmi *eeepc)
493{
494 if (eeepc->backlight_device)
495 backlight_device_unregister(eeepc->backlight_device);
496
497 eeepc->backlight_device = NULL;
498}
499
500static void eeepc_wmi_notify(u32 value, void *context)
501{
502 struct eeepc_wmi *eeepc = context;
503 struct acpi_buffer response = { ACPI_ALLOCATE_BUFFER, NULL };
504 union acpi_object *obj;
505 acpi_status status;
506 int code;
507 int orig_code;
508
509 status = wmi_get_event_data(value, &response);
510 if (status != AE_OK) {
511 pr_err("bad event status 0x%x\n", status);
512 return;
513 }
514
515 obj = (union acpi_object *)response.pointer;
516
517 if (obj && obj->type == ACPI_TYPE_INTEGER) {
518 code = obj->integer.value;
519 orig_code = code;
520
521 if (code >= NOTIFY_BRNUP_MIN && code <= NOTIFY_BRNUP_MAX)
522 code = NOTIFY_BRNUP_MIN;
523 else if (code >= NOTIFY_BRNDOWN_MIN &&
524 code <= NOTIFY_BRNDOWN_MAX)
525 code = NOTIFY_BRNDOWN_MIN;
526
527 if (code == NOTIFY_BRNUP_MIN || code == NOTIFY_BRNDOWN_MIN) {
528 if (!acpi_video_backlight_support())
529 eeepc_wmi_backlight_notify(eeepc, orig_code);
530 }
531
532 if (!sparse_keymap_report_event(eeepc->inputdev,
533 code, 1, true))
534 pr_info("Unknown key %x pressed\n", code);
535 }
536
537 kfree(obj);
538}
539
Dmitry Torokhov67fa38e2010-11-03 11:14:01 -0700540static ssize_t store_cpufv(struct device *dev, struct device_attribute *attr,
541 const char *buf, size_t count)
Chris Bagwell7f80d732010-10-11 18:47:18 -0500542{
543 int value;
544 struct acpi_buffer input = { (acpi_size)sizeof(value), &value };
545 acpi_status status;
546
547 if (!count || sscanf(buf, "%i", &value) != 1)
548 return -EINVAL;
549 if (value < 0 || value > 2)
550 return -EINVAL;
551
552 status = wmi_evaluate_method(EEEPC_WMI_MGMT_GUID,
553 1, EEEPC_WMI_METHODID_CFVS, &input, NULL);
554
555 if (ACPI_FAILURE(status))
556 return -EIO;
557 else
558 return count;
559}
560
561static DEVICE_ATTR(cpufv, S_IRUGO | S_IWUSR, NULL, store_cpufv);
562
563static void eeepc_wmi_sysfs_exit(struct platform_device *device)
564{
565 device_remove_file(&device->dev, &dev_attr_cpufv);
566}
567
568static int eeepc_wmi_sysfs_init(struct platform_device *device)
569{
570 int retval = -ENOMEM;
571
572 retval = device_create_file(&device->dev, &dev_attr_cpufv);
573 if (retval)
574 goto error_sysfs;
575
576 return 0;
577
578error_sysfs:
Corentin Chary27c136c2010-11-29 08:14:05 +0100579 eeepc_wmi_sysfs_exit(device);
Chris Bagwell7f80d732010-10-11 18:47:18 -0500580 return retval;
581}
582
Corentin Chary27c136c2010-11-29 08:14:05 +0100583/*
584 * Platform device
585 */
586static int __init eeepc_wmi_platform_init(struct eeepc_wmi *eeepc)
587{
588 int err;
589
590 eeepc->platform_device = platform_device_alloc(EEEPC_WMI_FILE, -1);
591 if (!eeepc->platform_device)
592 return -ENOMEM;
593 platform_set_drvdata(eeepc->platform_device, eeepc);
594
595 err = platform_device_add(eeepc->platform_device);
596 if (err)
597 goto fail_platform_device;
598
599 err = eeepc_wmi_sysfs_init(eeepc->platform_device);
600 if (err)
601 goto fail_sysfs;
602 return 0;
603
604fail_sysfs:
605 platform_device_del(eeepc->platform_device);
606fail_platform_device:
607 platform_device_put(eeepc->platform_device);
608 return err;
609}
610
611static void eeepc_wmi_platform_exit(struct eeepc_wmi *eeepc)
612{
613 eeepc_wmi_sysfs_exit(eeepc->platform_device);
614 platform_device_unregister(eeepc->platform_device);
615}
616
617/*
618 * WMI Driver
619 */
620static struct platform_device * __init eeepc_wmi_add(void)
Yong Wangee027e42010-03-21 10:26:34 +0800621{
Yong Wang45f2c692010-04-11 09:27:19 +0800622 struct eeepc_wmi *eeepc;
Yong Wangee027e42010-03-21 10:26:34 +0800623 acpi_status status;
Corentin Chary27c136c2010-11-29 08:14:05 +0100624 int err;
Yong Wangee027e42010-03-21 10:26:34 +0800625
Corentin Chary27c136c2010-11-29 08:14:05 +0100626 eeepc = kzalloc(sizeof(struct eeepc_wmi), GFP_KERNEL);
627 if (!eeepc)
628 return ERR_PTR(-ENOMEM);
629
630 /*
631 * Register the platform device first. It is used as a parent for the
632 * sub-devices below.
633 */
634 err = eeepc_wmi_platform_init(eeepc);
635 if (err)
636 goto fail_platform;
Yong Wang45f2c692010-04-11 09:27:19 +0800637
638 err = eeepc_wmi_input_init(eeepc);
639 if (err)
Corentin Chary27c136c2010-11-29 08:14:05 +0100640 goto fail_input;
Yong Wang3d7b1652010-04-11 09:27:54 +0800641
Corentin Chary084fca62010-11-29 08:14:06 +0100642 err = eeepc_wmi_led_init(eeepc);
643 if (err)
644 goto fail_leds;
645
Corentin Charyba48fdb2010-11-29 08:14:07 +0100646 err = eeepc_wmi_rfkill_init(eeepc);
647 if (err)
648 goto fail_rfkill;
649
Yong Wang3d7b1652010-04-11 09:27:54 +0800650 if (!acpi_video_backlight_support()) {
651 err = eeepc_wmi_backlight_init(eeepc);
652 if (err)
Corentin Chary27c136c2010-11-29 08:14:05 +0100653 goto fail_backlight;
Yong Wang3d7b1652010-04-11 09:27:54 +0800654 } else
655 pr_info("Backlight controlled by ACPI video driver\n");
Yong Wang45f2c692010-04-11 09:27:19 +0800656
657 status = wmi_install_notify_handler(EEEPC_WMI_EVENT_GUID,
Corentin Chary27c136c2010-11-29 08:14:05 +0100658 eeepc_wmi_notify, eeepc);
Yong Wang45f2c692010-04-11 09:27:19 +0800659 if (ACPI_FAILURE(status)) {
660 pr_err("Unable to register notify handler - %d\n",
661 status);
662 err = -ENODEV;
Corentin Chary27c136c2010-11-29 08:14:05 +0100663 goto fail_wmi_handler;
Yong Wang45f2c692010-04-11 09:27:19 +0800664 }
665
Corentin Chary27c136c2010-11-29 08:14:05 +0100666 return eeepc->platform_device;
Yong Wang45f2c692010-04-11 09:27:19 +0800667
Corentin Chary27c136c2010-11-29 08:14:05 +0100668fail_wmi_handler:
Yong Wang3d7b1652010-04-11 09:27:54 +0800669 eeepc_wmi_backlight_exit(eeepc);
Corentin Chary27c136c2010-11-29 08:14:05 +0100670fail_backlight:
Corentin Charyba48fdb2010-11-29 08:14:07 +0100671 eeepc_wmi_rfkill_exit(eeepc);
672fail_rfkill:
Corentin Chary084fca62010-11-29 08:14:06 +0100673 eeepc_wmi_led_exit(eeepc);
674fail_leds:
Yong Wang45f2c692010-04-11 09:27:19 +0800675 eeepc_wmi_input_exit(eeepc);
Corentin Chary27c136c2010-11-29 08:14:05 +0100676fail_input:
677 eeepc_wmi_platform_exit(eeepc);
678fail_platform:
679 kfree(eeepc);
680 return ERR_PTR(err);
Yong Wang45f2c692010-04-11 09:27:19 +0800681}
682
Corentin Chary27c136c2010-11-29 08:14:05 +0100683static int eeepc_wmi_remove(struct platform_device *device)
Yong Wang45f2c692010-04-11 09:27:19 +0800684{
685 struct eeepc_wmi *eeepc;
686
687 eeepc = platform_get_drvdata(device);
688 wmi_remove_notify_handler(EEEPC_WMI_EVENT_GUID);
Yong Wang3d7b1652010-04-11 09:27:54 +0800689 eeepc_wmi_backlight_exit(eeepc);
Yong Wang45f2c692010-04-11 09:27:19 +0800690 eeepc_wmi_input_exit(eeepc);
Corentin Chary084fca62010-11-29 08:14:06 +0100691 eeepc_wmi_led_exit(eeepc);
Corentin Charyba48fdb2010-11-29 08:14:07 +0100692 eeepc_wmi_rfkill_exit(eeepc);
Corentin Chary27c136c2010-11-29 08:14:05 +0100693 eeepc_wmi_platform_exit(eeepc);
Yong Wang45f2c692010-04-11 09:27:19 +0800694
Corentin Chary27c136c2010-11-29 08:14:05 +0100695 kfree(eeepc);
Yong Wang45f2c692010-04-11 09:27:19 +0800696 return 0;
697}
698
699static struct platform_driver platform_driver = {
700 .driver = {
701 .name = EEEPC_WMI_FILE,
702 .owner = THIS_MODULE,
703 },
Yong Wang45f2c692010-04-11 09:27:19 +0800704};
705
706static int __init eeepc_wmi_init(void)
707{
Yong Wang45f2c692010-04-11 09:27:19 +0800708 int err;
709
Yong Wang3d7b1652010-04-11 09:27:54 +0800710 if (!wmi_has_guid(EEEPC_WMI_EVENT_GUID) ||
711 !wmi_has_guid(EEEPC_WMI_MGMT_GUID)) {
Yong Wang81248882010-04-11 09:26:33 +0800712 pr_warning("No known WMI GUID found\n");
Yong Wangee027e42010-03-21 10:26:34 +0800713 return -ENODEV;
714 }
715
Corentin Chary27c136c2010-11-29 08:14:05 +0100716 platform_device = eeepc_wmi_add();
717 if (IS_ERR(platform_device)) {
718 err = PTR_ERR(platform_device);
719 goto fail_eeepc_wmi;
Yong Wang81248882010-04-11 09:26:33 +0800720 }
Yong Wangee027e42010-03-21 10:26:34 +0800721
Yong Wang45f2c692010-04-11 09:27:19 +0800722 err = platform_driver_register(&platform_driver);
723 if (err) {
724 pr_warning("Unable to register platform driver\n");
Corentin Chary27c136c2010-11-29 08:14:05 +0100725 goto fail_platform_driver;
Yong Wangee027e42010-03-21 10:26:34 +0800726 }
727
728 return 0;
Yong Wang45f2c692010-04-11 09:27:19 +0800729
Corentin Chary27c136c2010-11-29 08:14:05 +0100730fail_platform_driver:
731 eeepc_wmi_remove(platform_device);
732fail_eeepc_wmi:
Yong Wang45f2c692010-04-11 09:27:19 +0800733 return err;
Yong Wangee027e42010-03-21 10:26:34 +0800734}
735
736static void __exit eeepc_wmi_exit(void)
737{
Corentin Chary27c136c2010-11-29 08:14:05 +0100738 eeepc_wmi_remove(platform_device);
Yong Wang45f2c692010-04-11 09:27:19 +0800739 platform_driver_unregister(&platform_driver);
Yong Wangee027e42010-03-21 10:26:34 +0800740}
741
742module_init(eeepc_wmi_init);
743module_exit(eeepc_wmi_exit);