xref: /openbmc/linux/drivers/platform/x86/eeepc-wmi.c (revision 8571d75d)
1 /*
2  * Eee PC WMI hotkey driver
3  *
4  * Copyright(C) 2010 Intel Corporation.
5  * Copyright(C) 2010 Corentin Chary <corentin.chary@gmail.com>
6  *
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 
27 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
28 
29 #include <linux/kernel.h>
30 #include <linux/module.h>
31 #include <linux/init.h>
32 #include <linux/types.h>
33 #include <linux/slab.h>
34 #include <linux/input.h>
35 #include <linux/input/sparse-keymap.h>
36 #include <linux/fb.h>
37 #include <linux/backlight.h>
38 #include <linux/leds.h>
39 #include <linux/rfkill.h>
40 #include <linux/pci.h>
41 #include <linux/pci_hotplug.h>
42 #include <linux/debugfs.h>
43 #include <linux/seq_file.h>
44 #include <linux/platform_device.h>
45 #include <linux/dmi.h>
46 #include <acpi/acpi_bus.h>
47 #include <acpi/acpi_drivers.h>
48 
49 #define	EEEPC_WMI_FILE	"eeepc-wmi"
50 
51 MODULE_AUTHOR("Yong Wang <yong.y.wang@intel.com>");
52 MODULE_DESCRIPTION("Eee PC WMI Hotkey Driver");
53 MODULE_LICENSE("GPL");
54 
55 #define EEEPC_ACPI_HID		"ASUS010" /* old _HID used in eeepc-laptop */
56 
57 #define EEEPC_WMI_EVENT_GUID	"ABBC0F72-8EA1-11D1-00A0-C90629100000"
58 #define EEEPC_WMI_MGMT_GUID	"97845ED0-4E6D-11DE-8A39-0800200C9A66"
59 
60 MODULE_ALIAS("wmi:"EEEPC_WMI_EVENT_GUID);
61 MODULE_ALIAS("wmi:"EEEPC_WMI_MGMT_GUID);
62 
63 #define NOTIFY_BRNUP_MIN		0x11
64 #define NOTIFY_BRNUP_MAX		0x1f
65 #define NOTIFY_BRNDOWN_MIN		0x20
66 #define NOTIFY_BRNDOWN_MAX		0x2e
67 
68 #define EEEPC_WMI_METHODID_DSTS		0x53544344
69 #define EEEPC_WMI_METHODID_DEVS		0x53564544
70 #define EEEPC_WMI_METHODID_CFVS		0x53564643
71 
72 #define EEEPC_WMI_DEVID_WLAN		0x00010011
73 #define EEEPC_WMI_DEVID_BLUETOOTH	0x00010013
74 #define EEEPC_WMI_DEVID_WIMAX		0x00010017
75 #define EEEPC_WMI_DEVID_WWAN3G		0x00010019
76 #define EEEPC_WMI_DEVID_BACKLIGHT	0x00050011
77 #define EEEPC_WMI_DEVID_BRIGHTNESS	0x00050012
78 #define EEEPC_WMI_DEVID_CAMERA		0x00060013
79 #define EEEPC_WMI_DEVID_CARDREADER	0x00080013
80 #define EEEPC_WMI_DEVID_TOUCHPAD_LED	0x00100012
81 
82 #define EEEPC_WMI_DSTS_STATUS_BIT	0x00000001
83 #define EEEPC_WMI_DSTS_PRESENCE_BIT	0x00010000
84 #define EEEPC_WMI_DSTS_BRIGHTNESS_MASK	0x000000FF
85 #define EEEPC_WMI_DSTS_MAX_BRIGTH_MASK	0x0000FF00
86 
87 static bool hotplug_wireless;
88 
89 module_param(hotplug_wireless, bool, 0444);
90 MODULE_PARM_DESC(hotplug_wireless,
91 		 "Enable hotplug for wireless device. "
92 		 "If your laptop needs that, please report to "
93 		 "acpi4asus-user@lists.sourceforge.net.");
94 
95 static const struct key_entry eeepc_wmi_keymap[] = {
96 	/* Sleep already handled via generic ACPI code */
97 	{ KE_IGNORE, NOTIFY_BRNDOWN_MIN, { KEY_BRIGHTNESSDOWN } },
98 	{ KE_IGNORE, NOTIFY_BRNUP_MIN, { KEY_BRIGHTNESSUP } },
99 	{ KE_KEY, 0x30, { KEY_VOLUMEUP } },
100 	{ KE_KEY, 0x31, { KEY_VOLUMEDOWN } },
101 	{ KE_KEY, 0x32, { KEY_MUTE } },
102 	{ KE_KEY, 0x5c, { KEY_F15 } },
103 	{ KE_KEY, 0x5d, { KEY_WLAN } },
104 	{ KE_KEY, 0x6b, { KEY_F13 } }, /* Disable Touchpad */
105 	{ KE_KEY, 0x88, { KEY_WLAN } },
106 	{ KE_KEY, 0xcc, { KEY_SWITCHVIDEOMODE } },
107 	{ KE_KEY, 0xe0, { KEY_PROG1 } },
108 	{ KE_KEY, 0xe1, { KEY_F14 } },
109 	{ KE_KEY, 0xe9, { KEY_BRIGHTNESS_ZERO } },
110 	{ KE_END, 0},
111 };
112 
113 struct bios_args {
114 	u32	dev_id;
115 	u32	ctrl_param;
116 };
117 
118 /*
119  * eeepc-wmi/    - debugfs root directory
120  *   dev_id      - current dev_id
121  *   ctrl_param  - current ctrl_param
122  *   devs        - call DEVS(dev_id, ctrl_param) and print result
123  *   dsts        - call DSTS(dev_id)  and print result
124  */
125 struct eeepc_wmi_debug {
126 	struct dentry *root;
127 	u32 dev_id;
128 	u32 ctrl_param;
129 };
130 
131 struct eeepc_wmi {
132 	bool hotplug_wireless;
133 
134 	struct input_dev *inputdev;
135 	struct backlight_device *backlight_device;
136 	struct platform_device *platform_device;
137 
138 	struct led_classdev tpd_led;
139 	int tpd_led_wk;
140 	struct workqueue_struct *led_workqueue;
141 	struct work_struct tpd_led_work;
142 
143 	struct rfkill *wlan_rfkill;
144 	struct rfkill *bluetooth_rfkill;
145 	struct rfkill *wimax_rfkill;
146 	struct rfkill *wwan3g_rfkill;
147 
148 	struct hotplug_slot *hotplug_slot;
149 	struct mutex hotplug_lock;
150 	struct mutex wmi_lock;
151 	struct workqueue_struct *hotplug_workqueue;
152 	struct work_struct hotplug_work;
153 
154 	struct eeepc_wmi_debug debug;
155 };
156 
157 static int eeepc_wmi_input_init(struct eeepc_wmi *eeepc)
158 {
159 	int err;
160 
161 	eeepc->inputdev = input_allocate_device();
162 	if (!eeepc->inputdev)
163 		return -ENOMEM;
164 
165 	eeepc->inputdev->name = "Eee PC WMI hotkeys";
166 	eeepc->inputdev->phys = EEEPC_WMI_FILE "/input0";
167 	eeepc->inputdev->id.bustype = BUS_HOST;
168 	eeepc->inputdev->dev.parent = &eeepc->platform_device->dev;
169 
170 	err = sparse_keymap_setup(eeepc->inputdev, eeepc_wmi_keymap, NULL);
171 	if (err)
172 		goto err_free_dev;
173 
174 	err = input_register_device(eeepc->inputdev);
175 	if (err)
176 		goto err_free_keymap;
177 
178 	return 0;
179 
180 err_free_keymap:
181 	sparse_keymap_free(eeepc->inputdev);
182 err_free_dev:
183 	input_free_device(eeepc->inputdev);
184 	return err;
185 }
186 
187 static void eeepc_wmi_input_exit(struct eeepc_wmi *eeepc)
188 {
189 	if (eeepc->inputdev) {
190 		sparse_keymap_free(eeepc->inputdev);
191 		input_unregister_device(eeepc->inputdev);
192 	}
193 
194 	eeepc->inputdev = NULL;
195 }
196 
197 static acpi_status eeepc_wmi_get_devstate(u32 dev_id, u32 *retval)
198 {
199 	struct acpi_buffer input = { (acpi_size)sizeof(u32), &dev_id };
200 	struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER, NULL };
201 	union acpi_object *obj;
202 	acpi_status status;
203 	u32 tmp;
204 
205 	status = wmi_evaluate_method(EEEPC_WMI_MGMT_GUID,
206 				     1, EEEPC_WMI_METHODID_DSTS,
207 				     &input, &output);
208 
209 	if (ACPI_FAILURE(status))
210 		return status;
211 
212 	obj = (union acpi_object *)output.pointer;
213 	if (obj && obj->type == ACPI_TYPE_INTEGER)
214 		tmp = (u32)obj->integer.value;
215 	else
216 		tmp = 0;
217 
218 	if (retval)
219 		*retval = tmp;
220 
221 	kfree(obj);
222 
223 	return status;
224 
225 }
226 
227 static acpi_status eeepc_wmi_set_devstate(u32 dev_id, u32 ctrl_param,
228 					  u32 *retval)
229 {
230 	struct bios_args args = {
231 		.dev_id = dev_id,
232 		.ctrl_param = ctrl_param,
233 	};
234 	struct acpi_buffer input = { (acpi_size)sizeof(args), &args };
235 	acpi_status status;
236 
237 	if (!retval) {
238 		status = wmi_evaluate_method(EEEPC_WMI_MGMT_GUID, 1,
239 					     EEEPC_WMI_METHODID_DEVS,
240 					     &input, NULL);
241 	} else {
242 		struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER, NULL };
243 		union acpi_object *obj;
244 		u32 tmp;
245 
246 		status = wmi_evaluate_method(EEEPC_WMI_MGMT_GUID, 1,
247 					     EEEPC_WMI_METHODID_DEVS,
248 					     &input, &output);
249 
250 		if (ACPI_FAILURE(status))
251 			return status;
252 
253 		obj = (union acpi_object *)output.pointer;
254 		if (obj && obj->type == ACPI_TYPE_INTEGER)
255 			tmp = (u32)obj->integer.value;
256 		else
257 			tmp = 0;
258 
259 		*retval = tmp;
260 
261 		kfree(obj);
262 	}
263 
264 	return status;
265 }
266 
267 /* Helper for special devices with magic return codes */
268 static int eeepc_wmi_get_devstate_bits(u32 dev_id, u32 mask)
269 {
270 	u32 retval = 0;
271 	acpi_status status;
272 
273 	status = eeepc_wmi_get_devstate(dev_id, &retval);
274 
275 	if (ACPI_FAILURE(status))
276 		return -EINVAL;
277 
278 	if (!(retval & EEEPC_WMI_DSTS_PRESENCE_BIT))
279 		return -ENODEV;
280 
281 	return retval & mask;
282 }
283 
284 static int eeepc_wmi_get_devstate_simple(u32 dev_id)
285 {
286 	return eeepc_wmi_get_devstate_bits(dev_id, EEEPC_WMI_DSTS_STATUS_BIT);
287 }
288 
289 /*
290  * LEDs
291  */
292 /*
293  * These functions actually update the LED's, and are called from a
294  * workqueue. By doing this as separate work rather than when the LED
295  * subsystem asks, we avoid messing with the Eeepc ACPI stuff during a
296  * potentially bad time, such as a timer interrupt.
297  */
298 static void tpd_led_update(struct work_struct *work)
299 {
300 	int ctrl_param;
301 	struct eeepc_wmi *eeepc;
302 
303 	eeepc = container_of(work, struct eeepc_wmi, tpd_led_work);
304 
305 	ctrl_param = eeepc->tpd_led_wk;
306 	eeepc_wmi_set_devstate(EEEPC_WMI_DEVID_TOUCHPAD_LED, ctrl_param, NULL);
307 }
308 
309 static void tpd_led_set(struct led_classdev *led_cdev,
310 			enum led_brightness value)
311 {
312 	struct eeepc_wmi *eeepc;
313 
314 	eeepc = container_of(led_cdev, struct eeepc_wmi, tpd_led);
315 
316 	eeepc->tpd_led_wk = !!value;
317 	queue_work(eeepc->led_workqueue, &eeepc->tpd_led_work);
318 }
319 
320 static int read_tpd_led_state(struct eeepc_wmi *eeepc)
321 {
322 	return eeepc_wmi_get_devstate_simple(EEEPC_WMI_DEVID_TOUCHPAD_LED);
323 }
324 
325 static enum led_brightness tpd_led_get(struct led_classdev *led_cdev)
326 {
327 	struct eeepc_wmi *eeepc;
328 
329 	eeepc = container_of(led_cdev, struct eeepc_wmi, tpd_led);
330 
331 	return read_tpd_led_state(eeepc);
332 }
333 
334 static int eeepc_wmi_led_init(struct eeepc_wmi *eeepc)
335 {
336 	int rv;
337 
338 	if (read_tpd_led_state(eeepc) < 0)
339 		return 0;
340 
341 	eeepc->led_workqueue = create_singlethread_workqueue("led_workqueue");
342 	if (!eeepc->led_workqueue)
343 		return -ENOMEM;
344 	INIT_WORK(&eeepc->tpd_led_work, tpd_led_update);
345 
346 	eeepc->tpd_led.name = "eeepc::touchpad";
347 	eeepc->tpd_led.brightness_set = tpd_led_set;
348 	eeepc->tpd_led.brightness_get = tpd_led_get;
349 	eeepc->tpd_led.max_brightness = 1;
350 
351 	rv = led_classdev_register(&eeepc->platform_device->dev,
352 				   &eeepc->tpd_led);
353 	if (rv) {
354 		destroy_workqueue(eeepc->led_workqueue);
355 		return rv;
356 	}
357 
358 	return 0;
359 }
360 
361 static void eeepc_wmi_led_exit(struct eeepc_wmi *eeepc)
362 {
363 	if (eeepc->tpd_led.dev)
364 		led_classdev_unregister(&eeepc->tpd_led);
365 	if (eeepc->led_workqueue)
366 		destroy_workqueue(eeepc->led_workqueue);
367 }
368 
369 /*
370  * PCI hotplug (for wlan rfkill)
371  */
372 static bool eeepc_wlan_rfkill_blocked(struct eeepc_wmi *eeepc)
373 {
374 	int result = eeepc_wmi_get_devstate_simple(EEEPC_WMI_DEVID_WLAN);
375 
376 	if (result < 0)
377 		return false;
378 	return !result;
379 }
380 
381 static void eeepc_rfkill_hotplug(struct eeepc_wmi *eeepc)
382 {
383 	struct pci_dev *dev;
384 	struct pci_bus *bus;
385 	bool blocked;
386 	bool absent;
387 	u32 l;
388 
389 	mutex_lock(&eeepc->wmi_lock);
390 	blocked = eeepc_wlan_rfkill_blocked(eeepc);
391 	mutex_unlock(&eeepc->wmi_lock);
392 
393 	mutex_lock(&eeepc->hotplug_lock);
394 
395 	if (eeepc->wlan_rfkill)
396 		rfkill_set_sw_state(eeepc->wlan_rfkill, blocked);
397 
398 	if (eeepc->hotplug_slot) {
399 		bus = pci_find_bus(0, 1);
400 		if (!bus) {
401 			pr_warning("Unable to find PCI bus 1?\n");
402 			goto out_unlock;
403 		}
404 
405 		if (pci_bus_read_config_dword(bus, 0, PCI_VENDOR_ID, &l)) {
406 			pr_err("Unable to read PCI config space?\n");
407 			goto out_unlock;
408 		}
409 		absent = (l == 0xffffffff);
410 
411 		if (blocked != absent) {
412 			pr_warning("BIOS says wireless lan is %s, "
413 					"but the pci device is %s\n",
414 				blocked ? "blocked" : "unblocked",
415 				absent ? "absent" : "present");
416 			pr_warning("skipped wireless hotplug as probably "
417 					"inappropriate for this model\n");
418 			goto out_unlock;
419 		}
420 
421 		if (!blocked) {
422 			dev = pci_get_slot(bus, 0);
423 			if (dev) {
424 				/* Device already present */
425 				pci_dev_put(dev);
426 				goto out_unlock;
427 			}
428 			dev = pci_scan_single_device(bus, 0);
429 			if (dev) {
430 				pci_bus_assign_resources(bus);
431 				if (pci_bus_add_device(dev))
432 					pr_err("Unable to hotplug wifi\n");
433 			}
434 		} else {
435 			dev = pci_get_slot(bus, 0);
436 			if (dev) {
437 				pci_remove_bus_device(dev);
438 				pci_dev_put(dev);
439 			}
440 		}
441 	}
442 
443 out_unlock:
444 	mutex_unlock(&eeepc->hotplug_lock);
445 }
446 
447 static void eeepc_rfkill_notify(acpi_handle handle, u32 event, void *data)
448 {
449 	struct eeepc_wmi *eeepc = data;
450 
451 	if (event != ACPI_NOTIFY_BUS_CHECK)
452 		return;
453 
454 	/*
455 	 * We can't call directly eeepc_rfkill_hotplug because most
456 	 * of the time WMBC is still being executed and not reetrant.
457 	 * There is currently no way to tell ACPICA that  we want this
458 	 * method to be serialized, we schedule a eeepc_rfkill_hotplug
459 	 * call later, in a safer context.
460 	 */
461 	queue_work(eeepc->hotplug_workqueue, &eeepc->hotplug_work);
462 }
463 
464 static int eeepc_register_rfkill_notifier(struct eeepc_wmi *eeepc,
465 					  char *node)
466 {
467 	acpi_status status;
468 	acpi_handle handle;
469 
470 	status = acpi_get_handle(NULL, node, &handle);
471 
472 	if (ACPI_SUCCESS(status)) {
473 		status = acpi_install_notify_handler(handle,
474 						     ACPI_SYSTEM_NOTIFY,
475 						     eeepc_rfkill_notify,
476 						     eeepc);
477 		if (ACPI_FAILURE(status))
478 			pr_warning("Failed to register notify on %s\n", node);
479 	} else
480 		return -ENODEV;
481 
482 	return 0;
483 }
484 
485 static void eeepc_unregister_rfkill_notifier(struct eeepc_wmi *eeepc,
486 					     char *node)
487 {
488 	acpi_status status = AE_OK;
489 	acpi_handle handle;
490 
491 	status = acpi_get_handle(NULL, node, &handle);
492 
493 	if (ACPI_SUCCESS(status)) {
494 		status = acpi_remove_notify_handler(handle,
495 						     ACPI_SYSTEM_NOTIFY,
496 						     eeepc_rfkill_notify);
497 		if (ACPI_FAILURE(status))
498 			pr_err("Error removing rfkill notify handler %s\n",
499 				node);
500 	}
501 }
502 
503 static int eeepc_get_adapter_status(struct hotplug_slot *hotplug_slot,
504 				    u8 *value)
505 {
506 	int result = eeepc_wmi_get_devstate_simple(EEEPC_WMI_DEVID_WLAN);
507 
508 	if (result < 0)
509 		return result;
510 
511 	*value = !!result;
512 	return 0;
513 }
514 
515 static void eeepc_cleanup_pci_hotplug(struct hotplug_slot *hotplug_slot)
516 {
517 	kfree(hotplug_slot->info);
518 	kfree(hotplug_slot);
519 }
520 
521 static struct hotplug_slot_ops eeepc_hotplug_slot_ops = {
522 	.owner = THIS_MODULE,
523 	.get_adapter_status = eeepc_get_adapter_status,
524 	.get_power_status = eeepc_get_adapter_status,
525 };
526 
527 static void eeepc_hotplug_work(struct work_struct *work)
528 {
529 	struct eeepc_wmi *eeepc;
530 
531 	eeepc = container_of(work, struct eeepc_wmi, hotplug_work);
532 	eeepc_rfkill_hotplug(eeepc);
533 }
534 
535 static int eeepc_setup_pci_hotplug(struct eeepc_wmi *eeepc)
536 {
537 	int ret = -ENOMEM;
538 	struct pci_bus *bus = pci_find_bus(0, 1);
539 
540 	if (!bus) {
541 		pr_err("Unable to find wifi PCI bus\n");
542 		return -ENODEV;
543 	}
544 
545 	eeepc->hotplug_workqueue =
546 		create_singlethread_workqueue("hotplug_workqueue");
547 	if (!eeepc->hotplug_workqueue)
548 		goto error_workqueue;
549 
550 	INIT_WORK(&eeepc->hotplug_work, eeepc_hotplug_work);
551 
552 	eeepc->hotplug_slot = kzalloc(sizeof(struct hotplug_slot), GFP_KERNEL);
553 	if (!eeepc->hotplug_slot)
554 		goto error_slot;
555 
556 	eeepc->hotplug_slot->info = kzalloc(sizeof(struct hotplug_slot_info),
557 					    GFP_KERNEL);
558 	if (!eeepc->hotplug_slot->info)
559 		goto error_info;
560 
561 	eeepc->hotplug_slot->private = eeepc;
562 	eeepc->hotplug_slot->release = &eeepc_cleanup_pci_hotplug;
563 	eeepc->hotplug_slot->ops = &eeepc_hotplug_slot_ops;
564 	eeepc_get_adapter_status(eeepc->hotplug_slot,
565 				 &eeepc->hotplug_slot->info->adapter_status);
566 
567 	ret = pci_hp_register(eeepc->hotplug_slot, bus, 0, "eeepc-wifi");
568 	if (ret) {
569 		pr_err("Unable to register hotplug slot - %d\n", ret);
570 		goto error_register;
571 	}
572 
573 	return 0;
574 
575 error_register:
576 	kfree(eeepc->hotplug_slot->info);
577 error_info:
578 	kfree(eeepc->hotplug_slot);
579 	eeepc->hotplug_slot = NULL;
580 error_slot:
581 	destroy_workqueue(eeepc->hotplug_workqueue);
582 error_workqueue:
583 	return ret;
584 }
585 
586 /*
587  * Rfkill devices
588  */
589 static int eeepc_rfkill_set(void *data, bool blocked)
590 {
591 	int dev_id = (unsigned long)data;
592 	u32 ctrl_param = !blocked;
593 	acpi_status status;
594 
595 	status = eeepc_wmi_set_devstate(dev_id, ctrl_param, NULL);
596 
597 	if (ACPI_FAILURE(status))
598 		return -EIO;
599 
600 	return 0;
601 }
602 
603 static void eeepc_rfkill_query(struct rfkill *rfkill, void *data)
604 {
605 	int dev_id = (unsigned long)data;
606 	int result;
607 
608 	result = eeepc_wmi_get_devstate_simple(dev_id);
609 
610 	if (result < 0)
611 		return ;
612 
613 	rfkill_set_sw_state(rfkill, !result);
614 }
615 
616 static int eeepc_rfkill_wlan_set(void *data, bool blocked)
617 {
618 	struct eeepc_wmi *eeepc = data;
619 	int ret;
620 
621 	/*
622 	 * This handler is enabled only if hotplug is enabled.
623 	 * In this case, the eeepc_wmi_set_devstate() will
624 	 * trigger a wmi notification and we need to wait
625 	 * this call to finish before being able to call
626 	 * any wmi method
627 	 */
628 	mutex_lock(&eeepc->wmi_lock);
629 	ret = eeepc_rfkill_set((void *)(long)EEEPC_WMI_DEVID_WLAN, blocked);
630 	mutex_unlock(&eeepc->wmi_lock);
631 	return ret;
632 }
633 
634 static void eeepc_rfkill_wlan_query(struct rfkill *rfkill, void *data)
635 {
636 	eeepc_rfkill_query(rfkill, (void *)(long)EEEPC_WMI_DEVID_WLAN);
637 }
638 
639 static const struct rfkill_ops eeepc_rfkill_wlan_ops = {
640 	.set_block = eeepc_rfkill_wlan_set,
641 	.query = eeepc_rfkill_wlan_query,
642 };
643 
644 static const struct rfkill_ops eeepc_rfkill_ops = {
645 	.set_block = eeepc_rfkill_set,
646 	.query = eeepc_rfkill_query,
647 };
648 
649 static int eeepc_new_rfkill(struct eeepc_wmi *eeepc,
650 			    struct rfkill **rfkill,
651 			    const char *name,
652 			    enum rfkill_type type, int dev_id)
653 {
654 	int result = eeepc_wmi_get_devstate_simple(dev_id);
655 
656 	if (result < 0)
657 		return result;
658 
659 	if (dev_id == EEEPC_WMI_DEVID_WLAN && eeepc->hotplug_wireless)
660 		*rfkill = rfkill_alloc(name, &eeepc->platform_device->dev, type,
661 				       &eeepc_rfkill_wlan_ops, eeepc);
662 	else
663 		*rfkill = rfkill_alloc(name, &eeepc->platform_device->dev, type,
664 				       &eeepc_rfkill_ops, (void *)(long)dev_id);
665 
666 	if (!*rfkill)
667 		return -EINVAL;
668 
669 	rfkill_init_sw_state(*rfkill, !result);
670 	result = rfkill_register(*rfkill);
671 	if (result) {
672 		rfkill_destroy(*rfkill);
673 		*rfkill = NULL;
674 		return result;
675 	}
676 	return 0;
677 }
678 
679 static void eeepc_wmi_rfkill_exit(struct eeepc_wmi *eeepc)
680 {
681 	eeepc_unregister_rfkill_notifier(eeepc, "\\_SB.PCI0.P0P5");
682 	eeepc_unregister_rfkill_notifier(eeepc, "\\_SB.PCI0.P0P6");
683 	eeepc_unregister_rfkill_notifier(eeepc, "\\_SB.PCI0.P0P7");
684 	if (eeepc->wlan_rfkill) {
685 		rfkill_unregister(eeepc->wlan_rfkill);
686 		rfkill_destroy(eeepc->wlan_rfkill);
687 		eeepc->wlan_rfkill = NULL;
688 	}
689 	/*
690 	 * Refresh pci hotplug in case the rfkill state was changed after
691 	 * eeepc_unregister_rfkill_notifier()
692 	 */
693 	eeepc_rfkill_hotplug(eeepc);
694 	if (eeepc->hotplug_slot)
695 		pci_hp_deregister(eeepc->hotplug_slot);
696 	if (eeepc->hotplug_workqueue)
697 		destroy_workqueue(eeepc->hotplug_workqueue);
698 
699 	if (eeepc->bluetooth_rfkill) {
700 		rfkill_unregister(eeepc->bluetooth_rfkill);
701 		rfkill_destroy(eeepc->bluetooth_rfkill);
702 		eeepc->bluetooth_rfkill = NULL;
703 	}
704 	if (eeepc->wimax_rfkill) {
705 		rfkill_unregister(eeepc->wimax_rfkill);
706 		rfkill_destroy(eeepc->wimax_rfkill);
707 		eeepc->wimax_rfkill = NULL;
708 	}
709 	if (eeepc->wwan3g_rfkill) {
710 		rfkill_unregister(eeepc->wwan3g_rfkill);
711 		rfkill_destroy(eeepc->wwan3g_rfkill);
712 		eeepc->wwan3g_rfkill = NULL;
713 	}
714 }
715 
716 static int eeepc_wmi_rfkill_init(struct eeepc_wmi *eeepc)
717 {
718 	int result = 0;
719 
720 	mutex_init(&eeepc->hotplug_lock);
721 	mutex_init(&eeepc->wmi_lock);
722 
723 	result = eeepc_new_rfkill(eeepc, &eeepc->wlan_rfkill,
724 				  "eeepc-wlan", RFKILL_TYPE_WLAN,
725 				  EEEPC_WMI_DEVID_WLAN);
726 
727 	if (result && result != -ENODEV)
728 		goto exit;
729 
730 	result = eeepc_new_rfkill(eeepc, &eeepc->bluetooth_rfkill,
731 				  "eeepc-bluetooth", RFKILL_TYPE_BLUETOOTH,
732 				  EEEPC_WMI_DEVID_BLUETOOTH);
733 
734 	if (result && result != -ENODEV)
735 		goto exit;
736 
737 	result = eeepc_new_rfkill(eeepc, &eeepc->wimax_rfkill,
738 				  "eeepc-wimax", RFKILL_TYPE_WIMAX,
739 				  EEEPC_WMI_DEVID_WIMAX);
740 
741 	if (result && result != -ENODEV)
742 		goto exit;
743 
744 	result = eeepc_new_rfkill(eeepc, &eeepc->wwan3g_rfkill,
745 				  "eeepc-wwan3g", RFKILL_TYPE_WWAN,
746 				  EEEPC_WMI_DEVID_WWAN3G);
747 
748 	if (result && result != -ENODEV)
749 		goto exit;
750 
751 	if (!eeepc->hotplug_wireless)
752 		goto exit;
753 
754 	result = eeepc_setup_pci_hotplug(eeepc);
755 	/*
756 	 * If we get -EBUSY then something else is handling the PCI hotplug -
757 	 * don't fail in this case
758 	 */
759 	if (result == -EBUSY)
760 		result = 0;
761 
762 	eeepc_register_rfkill_notifier(eeepc, "\\_SB.PCI0.P0P5");
763 	eeepc_register_rfkill_notifier(eeepc, "\\_SB.PCI0.P0P6");
764 	eeepc_register_rfkill_notifier(eeepc, "\\_SB.PCI0.P0P7");
765 	/*
766 	 * Refresh pci hotplug in case the rfkill state was changed during
767 	 * setup.
768 	 */
769 	eeepc_rfkill_hotplug(eeepc);
770 
771 exit:
772 	if (result && result != -ENODEV)
773 		eeepc_wmi_rfkill_exit(eeepc);
774 
775 	if (result == -ENODEV)
776 		result = 0;
777 
778 	return result;
779 }
780 
781 /*
782  * Backlight
783  */
784 static int read_backlight_power(void)
785 {
786 	int ret = eeepc_wmi_get_devstate_simple(EEEPC_WMI_DEVID_BACKLIGHT);
787 
788 	if (ret < 0)
789 		return ret;
790 
791 	return ret ? FB_BLANK_UNBLANK : FB_BLANK_POWERDOWN;
792 }
793 
794 static int read_brightness(struct backlight_device *bd)
795 {
796 	u32 retval;
797 	acpi_status status;
798 
799 	status = eeepc_wmi_get_devstate(EEEPC_WMI_DEVID_BRIGHTNESS, &retval);
800 
801 	if (ACPI_FAILURE(status))
802 		return -EIO;
803 	else
804 		return retval & EEEPC_WMI_DSTS_BRIGHTNESS_MASK;
805 }
806 
807 static int update_bl_status(struct backlight_device *bd)
808 {
809 	u32 ctrl_param;
810 	acpi_status status;
811 	int power;
812 
813 	ctrl_param = bd->props.brightness;
814 
815 	status = eeepc_wmi_set_devstate(EEEPC_WMI_DEVID_BRIGHTNESS,
816 					ctrl_param, NULL);
817 
818 	if (ACPI_FAILURE(status))
819 		return -EIO;
820 
821 	power = read_backlight_power();
822 	if (power != -ENODEV && bd->props.power != power) {
823 		ctrl_param = !!(bd->props.power == FB_BLANK_UNBLANK);
824 		status = eeepc_wmi_set_devstate(EEEPC_WMI_DEVID_BACKLIGHT,
825 						ctrl_param, NULL);
826 
827 		if (ACPI_FAILURE(status))
828 			return -EIO;
829 	}
830 	return 0;
831 }
832 
833 static const struct backlight_ops eeepc_wmi_bl_ops = {
834 	.get_brightness = read_brightness,
835 	.update_status = update_bl_status,
836 };
837 
838 static int eeepc_wmi_backlight_notify(struct eeepc_wmi *eeepc, int code)
839 {
840 	struct backlight_device *bd = eeepc->backlight_device;
841 	int old = bd->props.brightness;
842 	int new = old;
843 
844 	if (code >= NOTIFY_BRNUP_MIN && code <= NOTIFY_BRNUP_MAX)
845 		new = code - NOTIFY_BRNUP_MIN + 1;
846 	else if (code >= NOTIFY_BRNDOWN_MIN && code <= NOTIFY_BRNDOWN_MAX)
847 		new = code - NOTIFY_BRNDOWN_MIN;
848 
849 	bd->props.brightness = new;
850 	backlight_update_status(bd);
851 	backlight_force_update(bd, BACKLIGHT_UPDATE_HOTKEY);
852 
853 	return old;
854 }
855 
856 static int eeepc_wmi_backlight_init(struct eeepc_wmi *eeepc)
857 {
858 	struct backlight_device *bd;
859 	struct backlight_properties props;
860 	int max;
861 	int power;
862 
863 	max = eeepc_wmi_get_devstate_bits(EEEPC_WMI_DEVID_BRIGHTNESS,
864 					  EEEPC_WMI_DSTS_MAX_BRIGTH_MASK);
865 	power = read_backlight_power();
866 
867 	if (max < 0 && power < 0) {
868 		/* Try to keep the original error */
869 		if (max == -ENODEV && power == -ENODEV)
870 			return -ENODEV;
871 		if (max != -ENODEV)
872 			return max;
873 		else
874 			return power;
875 	}
876 	if (max == -ENODEV)
877 		max = 0;
878 	if (power == -ENODEV)
879 		power = FB_BLANK_UNBLANK;
880 
881 	memset(&props, 0, sizeof(struct backlight_properties));
882 	props.max_brightness = max;
883 	bd = backlight_device_register(EEEPC_WMI_FILE,
884 				       &eeepc->platform_device->dev, eeepc,
885 				       &eeepc_wmi_bl_ops, &props);
886 	if (IS_ERR(bd)) {
887 		pr_err("Could not register backlight device\n");
888 		return PTR_ERR(bd);
889 	}
890 
891 	eeepc->backlight_device = bd;
892 
893 	bd->props.brightness = read_brightness(bd);
894 	bd->props.power = power;
895 	backlight_update_status(bd);
896 
897 	return 0;
898 }
899 
900 static void eeepc_wmi_backlight_exit(struct eeepc_wmi *eeepc)
901 {
902 	if (eeepc->backlight_device)
903 		backlight_device_unregister(eeepc->backlight_device);
904 
905 	eeepc->backlight_device = NULL;
906 }
907 
908 static void eeepc_wmi_notify(u32 value, void *context)
909 {
910 	struct eeepc_wmi *eeepc = context;
911 	struct acpi_buffer response = { ACPI_ALLOCATE_BUFFER, NULL };
912 	union acpi_object *obj;
913 	acpi_status status;
914 	int code;
915 	int orig_code;
916 
917 	status = wmi_get_event_data(value, &response);
918 	if (status != AE_OK) {
919 		pr_err("bad event status 0x%x\n", status);
920 		return;
921 	}
922 
923 	obj = (union acpi_object *)response.pointer;
924 
925 	if (obj && obj->type == ACPI_TYPE_INTEGER) {
926 		code = obj->integer.value;
927 		orig_code = code;
928 
929 		if (code >= NOTIFY_BRNUP_MIN && code <= NOTIFY_BRNUP_MAX)
930 			code = NOTIFY_BRNUP_MIN;
931 		else if (code >= NOTIFY_BRNDOWN_MIN &&
932 			 code <= NOTIFY_BRNDOWN_MAX)
933 			code = NOTIFY_BRNDOWN_MIN;
934 
935 		if (code == NOTIFY_BRNUP_MIN || code == NOTIFY_BRNDOWN_MIN) {
936 			if (!acpi_video_backlight_support())
937 				eeepc_wmi_backlight_notify(eeepc, orig_code);
938 		}
939 
940 		if (!sparse_keymap_report_event(eeepc->inputdev,
941 						code, 1, true))
942 			pr_info("Unknown key %x pressed\n", code);
943 	}
944 
945 	kfree(obj);
946 }
947 
948 /*
949  * Sys helpers
950  */
951 static int parse_arg(const char *buf, unsigned long count, int *val)
952 {
953 	if (!count)
954 		return 0;
955 	if (sscanf(buf, "%i", val) != 1)
956 		return -EINVAL;
957 	return count;
958 }
959 
960 static ssize_t store_sys_wmi(int devid, const char *buf, size_t count)
961 {
962 	acpi_status status;
963 	u32 retval;
964 	int rv, value;
965 
966 	value = eeepc_wmi_get_devstate_simple(devid);
967 	if (value == -ENODEV) /* Check device presence */
968 		return value;
969 
970 	rv = parse_arg(buf, count, &value);
971 	status = eeepc_wmi_set_devstate(devid, value, &retval);
972 
973 	if (ACPI_FAILURE(status))
974 		return -EIO;
975 	return rv;
976 }
977 
978 static ssize_t show_sys_wmi(int devid, char *buf)
979 {
980 	int value = eeepc_wmi_get_devstate_simple(devid);
981 
982 	if (value < 0)
983 		return value;
984 
985 	return sprintf(buf, "%d\n", value);
986 }
987 
988 #define EEEPC_WMI_CREATE_DEVICE_ATTR(_name, _mode, _cm)			\
989 	static ssize_t show_##_name(struct device *dev,			\
990 				    struct device_attribute *attr,	\
991 				    char *buf)				\
992 	{								\
993 		return show_sys_wmi(_cm, buf);				\
994 	}								\
995 	static ssize_t store_##_name(struct device *dev,		\
996 				     struct device_attribute *attr,	\
997 				     const char *buf, size_t count)	\
998 	{								\
999 		return store_sys_wmi(_cm, buf, count);			\
1000 	}								\
1001 	static struct device_attribute dev_attr_##_name = {		\
1002 		.attr = {						\
1003 			.name = __stringify(_name),			\
1004 			.mode = _mode },				\
1005 		.show   = show_##_name,					\
1006 		.store  = store_##_name,				\
1007 	}
1008 
1009 EEEPC_WMI_CREATE_DEVICE_ATTR(camera, 0644, EEEPC_WMI_DEVID_CAMERA);
1010 EEEPC_WMI_CREATE_DEVICE_ATTR(cardr, 0644, EEEPC_WMI_DEVID_CARDREADER);
1011 
1012 static ssize_t store_cpufv(struct device *dev, struct device_attribute *attr,
1013 			   const char *buf, size_t count)
1014 {
1015 	int value;
1016 	struct acpi_buffer input = { (acpi_size)sizeof(value), &value };
1017 	acpi_status status;
1018 
1019 	if (!count || sscanf(buf, "%i", &value) != 1)
1020 		return -EINVAL;
1021 	if (value < 0 || value > 2)
1022 		return -EINVAL;
1023 
1024 	status = wmi_evaluate_method(EEEPC_WMI_MGMT_GUID,
1025 				     1, EEEPC_WMI_METHODID_CFVS, &input, NULL);
1026 
1027 	if (ACPI_FAILURE(status))
1028 		return -EIO;
1029 	else
1030 		return count;
1031 }
1032 
1033 static DEVICE_ATTR(cpufv, S_IRUGO | S_IWUSR, NULL, store_cpufv);
1034 
1035 static struct attribute *platform_attributes[] = {
1036 	&dev_attr_cpufv.attr,
1037 	&dev_attr_camera.attr,
1038 	&dev_attr_cardr.attr,
1039 	NULL
1040 };
1041 
1042 static mode_t eeepc_sysfs_is_visible(struct kobject *kobj,
1043 				     struct attribute *attr,
1044 				     int idx)
1045 {
1046 	bool supported = true;
1047 	int devid = -1;
1048 
1049 	if (attr == &dev_attr_camera.attr)
1050 		devid = EEEPC_WMI_DEVID_CAMERA;
1051 	else if (attr == &dev_attr_cardr.attr)
1052 		devid = EEEPC_WMI_DEVID_CARDREADER;
1053 
1054 	if (devid != -1)
1055 		supported = eeepc_wmi_get_devstate_simple(devid) != -ENODEV;
1056 
1057 	return supported ? attr->mode : 0;
1058 }
1059 
1060 static struct attribute_group platform_attribute_group = {
1061 	.is_visible	= eeepc_sysfs_is_visible,
1062 	.attrs		= platform_attributes
1063 };
1064 
1065 static void eeepc_wmi_sysfs_exit(struct platform_device *device)
1066 {
1067 	sysfs_remove_group(&device->dev.kobj, &platform_attribute_group);
1068 }
1069 
1070 static int eeepc_wmi_sysfs_init(struct platform_device *device)
1071 {
1072 	return sysfs_create_group(&device->dev.kobj, &platform_attribute_group);
1073 }
1074 
1075 /*
1076  * Platform device
1077  */
1078 static int __init eeepc_wmi_platform_init(struct eeepc_wmi *eeepc)
1079 {
1080 	return eeepc_wmi_sysfs_init(eeepc->platform_device);
1081 }
1082 
1083 static void eeepc_wmi_platform_exit(struct eeepc_wmi *eeepc)
1084 {
1085 	eeepc_wmi_sysfs_exit(eeepc->platform_device);
1086 }
1087 
1088 /*
1089  * debugfs
1090  */
1091 struct eeepc_wmi_debugfs_node {
1092 	struct eeepc_wmi *eeepc;
1093 	char *name;
1094 	int (*show)(struct seq_file *m, void *data);
1095 };
1096 
1097 static int show_dsts(struct seq_file *m, void *data)
1098 {
1099 	struct eeepc_wmi *eeepc = m->private;
1100 	acpi_status status;
1101 	u32 retval = -1;
1102 
1103 	status = eeepc_wmi_get_devstate(eeepc->debug.dev_id, &retval);
1104 
1105 	if (ACPI_FAILURE(status))
1106 		return -EIO;
1107 
1108 	seq_printf(m, "DSTS(%x) = %x\n", eeepc->debug.dev_id, retval);
1109 
1110 	return 0;
1111 }
1112 
1113 static int show_devs(struct seq_file *m, void *data)
1114 {
1115 	struct eeepc_wmi *eeepc = m->private;
1116 	acpi_status status;
1117 	u32 retval = -1;
1118 
1119 	status = eeepc_wmi_set_devstate(eeepc->debug.dev_id,
1120 					eeepc->debug.ctrl_param, &retval);
1121 	if (ACPI_FAILURE(status))
1122 		return -EIO;
1123 
1124 	seq_printf(m, "DEVS(%x, %x) = %x\n", eeepc->debug.dev_id,
1125 		   eeepc->debug.ctrl_param, retval);
1126 
1127 	return 0;
1128 }
1129 
1130 static struct eeepc_wmi_debugfs_node eeepc_wmi_debug_files[] = {
1131 	{ NULL, "devs", show_devs },
1132 	{ NULL, "dsts", show_dsts },
1133 };
1134 
1135 static int eeepc_wmi_debugfs_open(struct inode *inode, struct file *file)
1136 {
1137 	struct eeepc_wmi_debugfs_node *node = inode->i_private;
1138 
1139 	return single_open(file, node->show, node->eeepc);
1140 }
1141 
1142 static const struct file_operations eeepc_wmi_debugfs_io_ops = {
1143 	.owner = THIS_MODULE,
1144 	.open  = eeepc_wmi_debugfs_open,
1145 	.read = seq_read,
1146 	.llseek = seq_lseek,
1147 	.release = single_release,
1148 };
1149 
1150 static void eeepc_wmi_debugfs_exit(struct eeepc_wmi *eeepc)
1151 {
1152 	debugfs_remove_recursive(eeepc->debug.root);
1153 }
1154 
1155 static int eeepc_wmi_debugfs_init(struct eeepc_wmi *eeepc)
1156 {
1157 	struct dentry *dent;
1158 	int i;
1159 
1160 	eeepc->debug.root = debugfs_create_dir(EEEPC_WMI_FILE, NULL);
1161 	if (!eeepc->debug.root) {
1162 		pr_err("failed to create debugfs directory");
1163 		goto error_debugfs;
1164 	}
1165 
1166 	dent = debugfs_create_x32("dev_id", S_IRUGO|S_IWUSR,
1167 				  eeepc->debug.root, &eeepc->debug.dev_id);
1168 	if (!dent)
1169 		goto error_debugfs;
1170 
1171 	dent = debugfs_create_x32("ctrl_param", S_IRUGO|S_IWUSR,
1172 				  eeepc->debug.root, &eeepc->debug.ctrl_param);
1173 	if (!dent)
1174 		goto error_debugfs;
1175 
1176 	for (i = 0; i < ARRAY_SIZE(eeepc_wmi_debug_files); i++) {
1177 		struct eeepc_wmi_debugfs_node *node = &eeepc_wmi_debug_files[i];
1178 
1179 		node->eeepc = eeepc;
1180 		dent = debugfs_create_file(node->name, S_IFREG | S_IRUGO,
1181 					   eeepc->debug.root, node,
1182 					   &eeepc_wmi_debugfs_io_ops);
1183 		if (!dent) {
1184 			pr_err("failed to create debug file: %s\n", node->name);
1185 			goto error_debugfs;
1186 		}
1187 	}
1188 
1189 	return 0;
1190 
1191 error_debugfs:
1192 	eeepc_wmi_debugfs_exit(eeepc);
1193 	return -ENOMEM;
1194 }
1195 
1196 /*
1197  * WMI Driver
1198  */
1199 static void eeepc_dmi_check(struct eeepc_wmi *eeepc)
1200 {
1201 	const char *model;
1202 
1203 	model = dmi_get_system_info(DMI_PRODUCT_NAME);
1204 	if (!model)
1205 		return;
1206 
1207 	/*
1208 	 * Whitelist for wlan hotplug
1209 	 *
1210 	 * Eeepc 1000H needs the current hotplug code to handle
1211 	 * Fn+F2 correctly. We may add other Eeepc here later, but
1212 	 * it seems that most of the laptops supported by eeepc-wmi
1213 	 * don't need to be on this list
1214 	 */
1215 	if (strcmp(model, "1000H") == 0) {
1216 		eeepc->hotplug_wireless = true;
1217 		pr_info("wlan hotplug enabled\n");
1218 	}
1219 }
1220 
1221 static int __init eeepc_wmi_add(struct platform_device *pdev)
1222 {
1223 	struct eeepc_wmi *eeepc;
1224 	acpi_status status;
1225 	int err;
1226 
1227 	eeepc = kzalloc(sizeof(struct eeepc_wmi), GFP_KERNEL);
1228 	if (!eeepc)
1229 		return -ENOMEM;
1230 
1231 	eeepc->platform_device = pdev;
1232 	platform_set_drvdata(eeepc->platform_device, eeepc);
1233 
1234 	eeepc->hotplug_wireless = hotplug_wireless;
1235 	eeepc_dmi_check(eeepc);
1236 
1237 	err = eeepc_wmi_platform_init(eeepc);
1238 	if (err)
1239 		goto fail_platform;
1240 
1241 	err = eeepc_wmi_input_init(eeepc);
1242 	if (err)
1243 		goto fail_input;
1244 
1245 	err = eeepc_wmi_led_init(eeepc);
1246 	if (err)
1247 		goto fail_leds;
1248 
1249 	err = eeepc_wmi_rfkill_init(eeepc);
1250 	if (err)
1251 		goto fail_rfkill;
1252 
1253 	if (!acpi_video_backlight_support()) {
1254 		err = eeepc_wmi_backlight_init(eeepc);
1255 		if (err && err != -ENODEV)
1256 			goto fail_backlight;
1257 	} else
1258 		pr_info("Backlight controlled by ACPI video driver\n");
1259 
1260 	status = wmi_install_notify_handler(EEEPC_WMI_EVENT_GUID,
1261 					    eeepc_wmi_notify, eeepc);
1262 	if (ACPI_FAILURE(status)) {
1263 		pr_err("Unable to register notify handler - %d\n",
1264 			status);
1265 		err = -ENODEV;
1266 		goto fail_wmi_handler;
1267 	}
1268 
1269 	err = eeepc_wmi_debugfs_init(eeepc);
1270 	if (err)
1271 		goto fail_debugfs;
1272 
1273 	return 0;
1274 
1275 fail_debugfs:
1276 	wmi_remove_notify_handler(EEEPC_WMI_EVENT_GUID);
1277 fail_wmi_handler:
1278 	eeepc_wmi_backlight_exit(eeepc);
1279 fail_backlight:
1280 	eeepc_wmi_rfkill_exit(eeepc);
1281 fail_rfkill:
1282 	eeepc_wmi_led_exit(eeepc);
1283 fail_leds:
1284 	eeepc_wmi_input_exit(eeepc);
1285 fail_input:
1286 	eeepc_wmi_platform_exit(eeepc);
1287 fail_platform:
1288 	kfree(eeepc);
1289 	return err;
1290 }
1291 
1292 static int __exit eeepc_wmi_remove(struct platform_device *device)
1293 {
1294 	struct eeepc_wmi *eeepc;
1295 
1296 	eeepc = platform_get_drvdata(device);
1297 	wmi_remove_notify_handler(EEEPC_WMI_EVENT_GUID);
1298 	eeepc_wmi_backlight_exit(eeepc);
1299 	eeepc_wmi_input_exit(eeepc);
1300 	eeepc_wmi_led_exit(eeepc);
1301 	eeepc_wmi_rfkill_exit(eeepc);
1302 	eeepc_wmi_debugfs_exit(eeepc);
1303 	eeepc_wmi_platform_exit(eeepc);
1304 
1305 	kfree(eeepc);
1306 	return 0;
1307 }
1308 
1309 /*
1310  * Platform driver - hibernate/resume callbacks
1311  */
1312 static int eeepc_hotk_thaw(struct device *device)
1313 {
1314 	struct eeepc_wmi *eeepc = dev_get_drvdata(device);
1315 
1316 	if (eeepc->wlan_rfkill) {
1317 		bool wlan;
1318 
1319 		/*
1320 		 * Work around bios bug - acpi _PTS turns off the wireless led
1321 		 * during suspend.  Normally it restores it on resume, but
1322 		 * we should kick it ourselves in case hibernation is aborted.
1323 		 */
1324 		wlan = eeepc_wmi_get_devstate_simple(EEEPC_WMI_DEVID_WLAN);
1325 		eeepc_wmi_set_devstate(EEEPC_WMI_DEVID_WLAN, wlan, NULL);
1326 	}
1327 
1328 	return 0;
1329 }
1330 
1331 static int eeepc_hotk_restore(struct device *device)
1332 {
1333 	struct eeepc_wmi *eeepc = dev_get_drvdata(device);
1334 	int bl;
1335 
1336 	/* Refresh both wlan rfkill state and pci hotplug */
1337 	if (eeepc->wlan_rfkill)
1338 		eeepc_rfkill_hotplug(eeepc);
1339 
1340 	if (eeepc->bluetooth_rfkill) {
1341 		bl = !eeepc_wmi_get_devstate_simple(EEEPC_WMI_DEVID_BLUETOOTH);
1342 		rfkill_set_sw_state(eeepc->bluetooth_rfkill, bl);
1343 	}
1344 	if (eeepc->wimax_rfkill) {
1345 		bl = !eeepc_wmi_get_devstate_simple(EEEPC_WMI_DEVID_WIMAX);
1346 		rfkill_set_sw_state(eeepc->wimax_rfkill, bl);
1347 	}
1348 	if (eeepc->wwan3g_rfkill) {
1349 		bl = !eeepc_wmi_get_devstate_simple(EEEPC_WMI_DEVID_WWAN3G);
1350 		rfkill_set_sw_state(eeepc->wwan3g_rfkill, bl);
1351 	}
1352 
1353 	return 0;
1354 }
1355 
1356 static const struct dev_pm_ops eeepc_pm_ops = {
1357 	.thaw = eeepc_hotk_thaw,
1358 	.restore = eeepc_hotk_restore,
1359 };
1360 
1361 static struct platform_driver platform_driver = {
1362 	.remove = __exit_p(eeepc_wmi_remove),
1363 	.driver = {
1364 		.name = EEEPC_WMI_FILE,
1365 		.owner = THIS_MODULE,
1366 		.pm = &eeepc_pm_ops,
1367 	},
1368 };
1369 
1370 static acpi_status __init eeepc_wmi_parse_device(acpi_handle handle, u32 level,
1371 						 void *context, void **retval)
1372 {
1373 	pr_warning("Found legacy ATKD device (%s)", EEEPC_ACPI_HID);
1374 	*(bool *)context = true;
1375 	return AE_CTRL_TERMINATE;
1376 }
1377 
1378 static int __init eeepc_wmi_check_atkd(void)
1379 {
1380 	acpi_status status;
1381 	bool found = false;
1382 
1383 	status = acpi_get_devices(EEEPC_ACPI_HID, eeepc_wmi_parse_device,
1384 				  &found, NULL);
1385 
1386 	if (ACPI_FAILURE(status) || !found)
1387 		return 0;
1388 	return -1;
1389 }
1390 
1391 static int __init eeepc_wmi_probe(struct platform_device *pdev)
1392 {
1393 	if (!wmi_has_guid(EEEPC_WMI_EVENT_GUID) ||
1394 	    !wmi_has_guid(EEEPC_WMI_MGMT_GUID)) {
1395 		pr_warning("No known WMI GUID found\n");
1396 		return -ENODEV;
1397 	}
1398 
1399 	if (eeepc_wmi_check_atkd()) {
1400 		pr_warning("WMI device present, but legacy ATKD device is also "
1401 			   "present and enabled.");
1402 		pr_warning("You probably booted with acpi_osi=\"Linux\" or "
1403 			   "acpi_osi=\"!Windows 2009\"");
1404 		pr_warning("Can't load eeepc-wmi, use default acpi_osi "
1405 			   "(preferred) or eeepc-laptop");
1406 		return -ENODEV;
1407 	}
1408 
1409 	return eeepc_wmi_add(pdev);
1410 }
1411 
1412 static struct platform_device *platform_device;
1413 
1414 static int __init eeepc_wmi_init(void)
1415 {
1416 	platform_device = platform_create_bundle(&platform_driver,
1417 						 eeepc_wmi_probe,
1418 						 NULL, 0, NULL, 0);
1419 	if (IS_ERR(platform_device))
1420 		return PTR_ERR(platform_device);
1421 	return 0;
1422 }
1423 
1424 static void __exit eeepc_wmi_exit(void)
1425 {
1426 	platform_device_unregister(platform_device);
1427 	platform_driver_unregister(&platform_driver);
1428 }
1429 
1430 module_init(eeepc_wmi_init);
1431 module_exit(eeepc_wmi_exit);
1432