xref: /openbmc/linux/drivers/platform/x86/asus-wmi.c (revision 2f8be0e5)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Asus PC WMI hotkey driver
4  *
5  * Copyright(C) 2010 Intel Corporation.
6  * Copyright(C) 2010-2011 Corentin Chary <corentin.chary@gmail.com>
7  *
8  * Portions based on wistron_btns.c:
9  * Copyright (C) 2005 Miloslav Trmac <mitr@volny.cz>
10  * Copyright (C) 2005 Bernhard Rosenkraenzer <bero@arklinux.org>
11  * Copyright (C) 2005 Dmitry Torokhov <dtor@mail.ru>
12  */
13 
14 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
15 
16 #include <linux/kernel.h>
17 #include <linux/module.h>
18 #include <linux/init.h>
19 #include <linux/types.h>
20 #include <linux/slab.h>
21 #include <linux/input.h>
22 #include <linux/input/sparse-keymap.h>
23 #include <linux/fb.h>
24 #include <linux/backlight.h>
25 #include <linux/leds.h>
26 #include <linux/rfkill.h>
27 #include <linux/pci.h>
28 #include <linux/pci_hotplug.h>
29 #include <linux/power_supply.h>
30 #include <linux/hwmon.h>
31 #include <linux/hwmon-sysfs.h>
32 #include <linux/debugfs.h>
33 #include <linux/seq_file.h>
34 #include <linux/platform_data/x86/asus-wmi.h>
35 #include <linux/platform_device.h>
36 #include <linux/acpi.h>
37 #include <linux/dmi.h>
38 #include <linux/units.h>
39 
40 #include <acpi/battery.h>
41 #include <acpi/video.h>
42 
43 #include "asus-wmi.h"
44 
45 MODULE_AUTHOR("Corentin Chary <corentin.chary@gmail.com>, "
46 	      "Yong Wang <yong.y.wang@intel.com>");
47 MODULE_DESCRIPTION("Asus Generic WMI Driver");
48 MODULE_LICENSE("GPL");
49 
50 #define to_asus_wmi_driver(pdrv)					\
51 	(container_of((pdrv), struct asus_wmi_driver, platform_driver))
52 
53 #define ASUS_WMI_MGMT_GUID	"97845ED0-4E6D-11DE-8A39-0800200C9A66"
54 
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 #define NOTIFY_FNLOCK_TOGGLE		0x4e
60 #define NOTIFY_KBD_DOCK_CHANGE		0x75
61 #define NOTIFY_KBD_BRTUP		0xc4
62 #define NOTIFY_KBD_BRTDWN		0xc5
63 #define NOTIFY_KBD_BRTTOGGLE		0xc7
64 #define NOTIFY_KBD_FBM			0x99
65 #define NOTIFY_KBD_TTP			0xae
66 
67 #define ASUS_WMI_FNLOCK_BIOS_DISABLED	BIT(0)
68 
69 #define ASUS_FAN_DESC			"cpu_fan"
70 #define ASUS_FAN_MFUN			0x13
71 #define ASUS_FAN_SFUN_READ		0x06
72 #define ASUS_FAN_SFUN_WRITE		0x07
73 
74 /* Based on standard hwmon pwmX_enable values */
75 #define ASUS_FAN_CTRL_FULLSPEED		0
76 #define ASUS_FAN_CTRL_MANUAL		1
77 #define ASUS_FAN_CTRL_AUTO		2
78 
79 #define ASUS_FAN_BOOST_MODE_NORMAL		0
80 #define ASUS_FAN_BOOST_MODE_OVERBOOST		1
81 #define ASUS_FAN_BOOST_MODE_OVERBOOST_MASK	0x01
82 #define ASUS_FAN_BOOST_MODE_SILENT		2
83 #define ASUS_FAN_BOOST_MODE_SILENT_MASK		0x02
84 #define ASUS_FAN_BOOST_MODES_MASK		0x03
85 
86 #define ASUS_THROTTLE_THERMAL_POLICY_DEFAULT	0
87 #define ASUS_THROTTLE_THERMAL_POLICY_OVERBOOST	1
88 #define ASUS_THROTTLE_THERMAL_POLICY_SILENT	2
89 
90 #define USB_INTEL_XUSB2PR		0xD0
91 #define PCI_DEVICE_ID_INTEL_LYNXPOINT_LP_XHCI	0x9c31
92 
93 #define ASUS_ACPI_UID_ASUSWMI		"ASUSWMI"
94 #define ASUS_ACPI_UID_ATK		"ATK"
95 
96 #define WMI_EVENT_QUEUE_SIZE		0x10
97 #define WMI_EVENT_QUEUE_END		0x1
98 #define WMI_EVENT_MASK			0xFFFF
99 /* The WMI hotkey event value is always the same. */
100 #define WMI_EVENT_VALUE_ATK		0xFF
101 
102 #define WMI_EVENT_MASK			0xFFFF
103 
104 static const char * const ashs_ids[] = { "ATK4001", "ATK4002", NULL };
105 
106 static bool ashs_present(void)
107 {
108 	int i = 0;
109 	while (ashs_ids[i]) {
110 		if (acpi_dev_found(ashs_ids[i++]))
111 			return true;
112 	}
113 	return false;
114 }
115 
116 struct bios_args {
117 	u32 arg0;
118 	u32 arg1;
119 	u32 arg2; /* At least TUF Gaming series uses 3 dword input buffer. */
120 	u32 arg4;
121 	u32 arg5;
122 } __packed;
123 
124 /*
125  * Struct that's used for all methods called via AGFN. Naming is
126  * identically to the AML code.
127  */
128 struct agfn_args {
129 	u16 mfun; /* probably "Multi-function" to be called */
130 	u16 sfun; /* probably "Sub-function" to be called */
131 	u16 len;  /* size of the hole struct, including subfunction fields */
132 	u8 stas;  /* not used by now */
133 	u8 err;   /* zero on success */
134 } __packed;
135 
136 /* struct used for calling fan read and write methods */
137 struct agfn_fan_args {
138 	struct agfn_args agfn;	/* common fields */
139 	u8 fan;			/* fan number: 0: set auto mode 1: 1st fan */
140 	u32 speed;		/* read: RPM/100 - write: 0-255 */
141 } __packed;
142 
143 /*
144  * <platform>/    - debugfs root directory
145  *   dev_id      - current dev_id
146  *   ctrl_param  - current ctrl_param
147  *   method_id   - current method_id
148  *   devs        - call DEVS(dev_id, ctrl_param) and print result
149  *   dsts        - call DSTS(dev_id)  and print result
150  *   call        - call method_id(dev_id, ctrl_param) and print result
151  */
152 struct asus_wmi_debug {
153 	struct dentry *root;
154 	u32 method_id;
155 	u32 dev_id;
156 	u32 ctrl_param;
157 };
158 
159 struct asus_rfkill {
160 	struct asus_wmi *asus;
161 	struct rfkill *rfkill;
162 	u32 dev_id;
163 };
164 
165 enum fan_type {
166 	FAN_TYPE_NONE = 0,
167 	FAN_TYPE_AGFN,		/* deprecated on newer platforms */
168 	FAN_TYPE_SPEC83,	/* starting in Spec 8.3, use CPU_FAN_CTRL */
169 };
170 
171 struct asus_wmi {
172 	int dsts_id;
173 	int spec;
174 	int sfun;
175 	bool wmi_event_queue;
176 
177 	struct input_dev *inputdev;
178 	struct backlight_device *backlight_device;
179 	struct platform_device *platform_device;
180 
181 	struct led_classdev wlan_led;
182 	int wlan_led_wk;
183 	struct led_classdev tpd_led;
184 	int tpd_led_wk;
185 	struct led_classdev kbd_led;
186 	int kbd_led_wk;
187 	struct led_classdev lightbar_led;
188 	int lightbar_led_wk;
189 	struct workqueue_struct *led_workqueue;
190 	struct work_struct tpd_led_work;
191 	struct work_struct wlan_led_work;
192 	struct work_struct lightbar_led_work;
193 
194 	struct asus_rfkill wlan;
195 	struct asus_rfkill bluetooth;
196 	struct asus_rfkill wimax;
197 	struct asus_rfkill wwan3g;
198 	struct asus_rfkill gps;
199 	struct asus_rfkill uwb;
200 
201 	enum fan_type fan_type;
202 	int fan_pwm_mode;
203 	int agfn_pwm;
204 
205 	bool fan_boost_mode_available;
206 	u8 fan_boost_mode_mask;
207 	u8 fan_boost_mode;
208 
209 	bool throttle_thermal_policy_available;
210 	u8 throttle_thermal_policy_mode;
211 
212 	// The RSOC controls the maximum charging percentage.
213 	bool battery_rsoc_available;
214 
215 	struct hotplug_slot hotplug_slot;
216 	struct mutex hotplug_lock;
217 	struct mutex wmi_lock;
218 	struct workqueue_struct *hotplug_workqueue;
219 	struct work_struct hotplug_work;
220 
221 	bool fnlock_locked;
222 
223 	struct asus_wmi_debug debug;
224 
225 	struct asus_wmi_driver *driver;
226 };
227 
228 /* WMI ************************************************************************/
229 
230 static int asus_wmi_evaluate_method3(u32 method_id,
231 		u32 arg0, u32 arg1, u32 arg2, u32 *retval)
232 {
233 	struct bios_args args = {
234 		.arg0 = arg0,
235 		.arg1 = arg1,
236 		.arg2 = arg2,
237 	};
238 	struct acpi_buffer input = { (acpi_size) sizeof(args), &args };
239 	struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER, NULL };
240 	acpi_status status;
241 	union acpi_object *obj;
242 	u32 tmp = 0;
243 
244 	status = wmi_evaluate_method(ASUS_WMI_MGMT_GUID, 0, method_id,
245 				     &input, &output);
246 
247 	if (ACPI_FAILURE(status))
248 		return -EIO;
249 
250 	obj = (union acpi_object *)output.pointer;
251 	if (obj && obj->type == ACPI_TYPE_INTEGER)
252 		tmp = (u32) obj->integer.value;
253 
254 	if (retval)
255 		*retval = tmp;
256 
257 	kfree(obj);
258 
259 	if (tmp == ASUS_WMI_UNSUPPORTED_METHOD)
260 		return -ENODEV;
261 
262 	return 0;
263 }
264 
265 int asus_wmi_evaluate_method(u32 method_id, u32 arg0, u32 arg1, u32 *retval)
266 {
267 	return asus_wmi_evaluate_method3(method_id, arg0, arg1, 0, retval);
268 }
269 EXPORT_SYMBOL_GPL(asus_wmi_evaluate_method);
270 
271 static int asus_wmi_evaluate_method_agfn(const struct acpi_buffer args)
272 {
273 	struct acpi_buffer input;
274 	u64 phys_addr;
275 	u32 retval;
276 	u32 status;
277 
278 	/*
279 	 * Copy to dma capable address otherwise memory corruption occurs as
280 	 * bios has to be able to access it.
281 	 */
282 	input.pointer = kmemdup(args.pointer, args.length, GFP_DMA | GFP_KERNEL);
283 	input.length = args.length;
284 	if (!input.pointer)
285 		return -ENOMEM;
286 	phys_addr = virt_to_phys(input.pointer);
287 
288 	status = asus_wmi_evaluate_method(ASUS_WMI_METHODID_AGFN,
289 					phys_addr, 0, &retval);
290 	if (!status)
291 		memcpy(args.pointer, input.pointer, args.length);
292 
293 	kfree(input.pointer);
294 	if (status)
295 		return -ENXIO;
296 
297 	return retval;
298 }
299 
300 static int asus_wmi_get_devstate(struct asus_wmi *asus, u32 dev_id, u32 *retval)
301 {
302 	return asus_wmi_evaluate_method(asus->dsts_id, dev_id, 0, retval);
303 }
304 
305 static int asus_wmi_set_devstate(u32 dev_id, u32 ctrl_param,
306 				 u32 *retval)
307 {
308 	return asus_wmi_evaluate_method(ASUS_WMI_METHODID_DEVS, dev_id,
309 					ctrl_param, retval);
310 }
311 
312 /* Helper for special devices with magic return codes */
313 static int asus_wmi_get_devstate_bits(struct asus_wmi *asus,
314 				      u32 dev_id, u32 mask)
315 {
316 	u32 retval = 0;
317 	int err;
318 
319 	err = asus_wmi_get_devstate(asus, dev_id, &retval);
320 	if (err < 0)
321 		return err;
322 
323 	if (!(retval & ASUS_WMI_DSTS_PRESENCE_BIT))
324 		return -ENODEV;
325 
326 	if (mask == ASUS_WMI_DSTS_STATUS_BIT) {
327 		if (retval & ASUS_WMI_DSTS_UNKNOWN_BIT)
328 			return -ENODEV;
329 	}
330 
331 	return retval & mask;
332 }
333 
334 static int asus_wmi_get_devstate_simple(struct asus_wmi *asus, u32 dev_id)
335 {
336 	return asus_wmi_get_devstate_bits(asus, dev_id,
337 					  ASUS_WMI_DSTS_STATUS_BIT);
338 }
339 
340 static bool asus_wmi_dev_is_present(struct asus_wmi *asus, u32 dev_id)
341 {
342 	u32 retval;
343 	int status = asus_wmi_get_devstate(asus, dev_id, &retval);
344 
345 	return status == 0 && (retval & ASUS_WMI_DSTS_PRESENCE_BIT);
346 }
347 
348 /* Input **********************************************************************/
349 
350 static int asus_wmi_input_init(struct asus_wmi *asus)
351 {
352 	int err, result;
353 
354 	asus->inputdev = input_allocate_device();
355 	if (!asus->inputdev)
356 		return -ENOMEM;
357 
358 	asus->inputdev->name = asus->driver->input_name;
359 	asus->inputdev->phys = asus->driver->input_phys;
360 	asus->inputdev->id.bustype = BUS_HOST;
361 	asus->inputdev->dev.parent = &asus->platform_device->dev;
362 	set_bit(EV_REP, asus->inputdev->evbit);
363 
364 	err = sparse_keymap_setup(asus->inputdev, asus->driver->keymap, NULL);
365 	if (err)
366 		goto err_free_dev;
367 
368 	result = asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_KBD_DOCK);
369 	if (result >= 0) {
370 		input_set_capability(asus->inputdev, EV_SW, SW_TABLET_MODE);
371 		input_report_switch(asus->inputdev, SW_TABLET_MODE, !result);
372 	} else if (result != -ENODEV) {
373 		pr_err("Error checking for keyboard-dock: %d\n", result);
374 	}
375 
376 	err = input_register_device(asus->inputdev);
377 	if (err)
378 		goto err_free_dev;
379 
380 	return 0;
381 
382 err_free_dev:
383 	input_free_device(asus->inputdev);
384 	return err;
385 }
386 
387 static void asus_wmi_input_exit(struct asus_wmi *asus)
388 {
389 	if (asus->inputdev)
390 		input_unregister_device(asus->inputdev);
391 
392 	asus->inputdev = NULL;
393 }
394 
395 /* Battery ********************************************************************/
396 
397 /* The battery maximum charging percentage */
398 static int charge_end_threshold;
399 
400 static ssize_t charge_control_end_threshold_store(struct device *dev,
401 						  struct device_attribute *attr,
402 						  const char *buf, size_t count)
403 {
404 	int value, ret, rv;
405 
406 	ret = kstrtouint(buf, 10, &value);
407 	if (ret)
408 		return ret;
409 
410 	if (value < 0 || value > 100)
411 		return -EINVAL;
412 
413 	ret = asus_wmi_set_devstate(ASUS_WMI_DEVID_RSOC, value, &rv);
414 	if (ret)
415 		return ret;
416 
417 	if (rv != 1)
418 		return -EIO;
419 
420 	/* There isn't any method in the DSDT to read the threshold, so we
421 	 * save the threshold.
422 	 */
423 	charge_end_threshold = value;
424 	return count;
425 }
426 
427 static ssize_t charge_control_end_threshold_show(struct device *device,
428 						 struct device_attribute *attr,
429 						 char *buf)
430 {
431 	return sprintf(buf, "%d\n", charge_end_threshold);
432 }
433 
434 static DEVICE_ATTR_RW(charge_control_end_threshold);
435 
436 static int asus_wmi_battery_add(struct power_supply *battery)
437 {
438 	/* The WMI method does not provide a way to specific a battery, so we
439 	 * just assume it is the first battery.
440 	 * Note: On some newer ASUS laptops (Zenbook UM431DA), the primary/first
441 	 * battery is named BATT.
442 	 */
443 	if (strcmp(battery->desc->name, "BAT0") != 0 &&
444 	    strcmp(battery->desc->name, "BAT1") != 0 &&
445 	    strcmp(battery->desc->name, "BATT") != 0)
446 		return -ENODEV;
447 
448 	if (device_create_file(&battery->dev,
449 	    &dev_attr_charge_control_end_threshold))
450 		return -ENODEV;
451 
452 	/* The charge threshold is only reset when the system is power cycled,
453 	 * and we can't get the current threshold so let set it to 100% when
454 	 * a battery is added.
455 	 */
456 	asus_wmi_set_devstate(ASUS_WMI_DEVID_RSOC, 100, NULL);
457 	charge_end_threshold = 100;
458 
459 	return 0;
460 }
461 
462 static int asus_wmi_battery_remove(struct power_supply *battery)
463 {
464 	device_remove_file(&battery->dev,
465 			   &dev_attr_charge_control_end_threshold);
466 	return 0;
467 }
468 
469 static struct acpi_battery_hook battery_hook = {
470 	.add_battery = asus_wmi_battery_add,
471 	.remove_battery = asus_wmi_battery_remove,
472 	.name = "ASUS Battery Extension",
473 };
474 
475 static void asus_wmi_battery_init(struct asus_wmi *asus)
476 {
477 	asus->battery_rsoc_available = false;
478 	if (asus_wmi_dev_is_present(asus, ASUS_WMI_DEVID_RSOC)) {
479 		asus->battery_rsoc_available = true;
480 		battery_hook_register(&battery_hook);
481 	}
482 }
483 
484 static void asus_wmi_battery_exit(struct asus_wmi *asus)
485 {
486 	if (asus->battery_rsoc_available)
487 		battery_hook_unregister(&battery_hook);
488 }
489 
490 /* LEDs ***********************************************************************/
491 
492 /*
493  * These functions actually update the LED's, and are called from a
494  * workqueue. By doing this as separate work rather than when the LED
495  * subsystem asks, we avoid messing with the Asus ACPI stuff during a
496  * potentially bad time, such as a timer interrupt.
497  */
498 static void tpd_led_update(struct work_struct *work)
499 {
500 	int ctrl_param;
501 	struct asus_wmi *asus;
502 
503 	asus = container_of(work, struct asus_wmi, tpd_led_work);
504 
505 	ctrl_param = asus->tpd_led_wk;
506 	asus_wmi_set_devstate(ASUS_WMI_DEVID_TOUCHPAD_LED, ctrl_param, NULL);
507 }
508 
509 static void tpd_led_set(struct led_classdev *led_cdev,
510 			enum led_brightness value)
511 {
512 	struct asus_wmi *asus;
513 
514 	asus = container_of(led_cdev, struct asus_wmi, tpd_led);
515 
516 	asus->tpd_led_wk = !!value;
517 	queue_work(asus->led_workqueue, &asus->tpd_led_work);
518 }
519 
520 static int read_tpd_led_state(struct asus_wmi *asus)
521 {
522 	return asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_TOUCHPAD_LED);
523 }
524 
525 static enum led_brightness tpd_led_get(struct led_classdev *led_cdev)
526 {
527 	struct asus_wmi *asus;
528 
529 	asus = container_of(led_cdev, struct asus_wmi, tpd_led);
530 
531 	return read_tpd_led_state(asus);
532 }
533 
534 static void kbd_led_update(struct asus_wmi *asus)
535 {
536 	int ctrl_param = 0;
537 
538 	ctrl_param = 0x80 | (asus->kbd_led_wk & 0x7F);
539 	asus_wmi_set_devstate(ASUS_WMI_DEVID_KBD_BACKLIGHT, ctrl_param, NULL);
540 }
541 
542 static int kbd_led_read(struct asus_wmi *asus, int *level, int *env)
543 {
544 	int retval;
545 
546 	/*
547 	 * bits 0-2: level
548 	 * bit 7: light on/off
549 	 * bit 8-10: environment (0: dark, 1: normal, 2: light)
550 	 * bit 17: status unknown
551 	 */
552 	retval = asus_wmi_get_devstate_bits(asus, ASUS_WMI_DEVID_KBD_BACKLIGHT,
553 					    0xFFFF);
554 
555 	/* Unknown status is considered as off */
556 	if (retval == 0x8000)
557 		retval = 0;
558 
559 	if (retval < 0)
560 		return retval;
561 
562 	if (level)
563 		*level = retval & 0x7F;
564 	if (env)
565 		*env = (retval >> 8) & 0x7F;
566 	return 0;
567 }
568 
569 static void do_kbd_led_set(struct led_classdev *led_cdev, int value)
570 {
571 	struct asus_wmi *asus;
572 	int max_level;
573 
574 	asus = container_of(led_cdev, struct asus_wmi, kbd_led);
575 	max_level = asus->kbd_led.max_brightness;
576 
577 	asus->kbd_led_wk = clamp_val(value, 0, max_level);
578 	kbd_led_update(asus);
579 }
580 
581 static void kbd_led_set(struct led_classdev *led_cdev,
582 			enum led_brightness value)
583 {
584 	/* Prevent disabling keyboard backlight on module unregister */
585 	if (led_cdev->flags & LED_UNREGISTERING)
586 		return;
587 
588 	do_kbd_led_set(led_cdev, value);
589 }
590 
591 static void kbd_led_set_by_kbd(struct asus_wmi *asus, enum led_brightness value)
592 {
593 	struct led_classdev *led_cdev = &asus->kbd_led;
594 
595 	do_kbd_led_set(led_cdev, value);
596 	led_classdev_notify_brightness_hw_changed(led_cdev, asus->kbd_led_wk);
597 }
598 
599 static enum led_brightness kbd_led_get(struct led_classdev *led_cdev)
600 {
601 	struct asus_wmi *asus;
602 	int retval, value;
603 
604 	asus = container_of(led_cdev, struct asus_wmi, kbd_led);
605 
606 	retval = kbd_led_read(asus, &value, NULL);
607 	if (retval < 0)
608 		return retval;
609 
610 	return value;
611 }
612 
613 static int wlan_led_unknown_state(struct asus_wmi *asus)
614 {
615 	u32 result;
616 
617 	asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_WIRELESS_LED, &result);
618 
619 	return result & ASUS_WMI_DSTS_UNKNOWN_BIT;
620 }
621 
622 static void wlan_led_update(struct work_struct *work)
623 {
624 	int ctrl_param;
625 	struct asus_wmi *asus;
626 
627 	asus = container_of(work, struct asus_wmi, wlan_led_work);
628 
629 	ctrl_param = asus->wlan_led_wk;
630 	asus_wmi_set_devstate(ASUS_WMI_DEVID_WIRELESS_LED, ctrl_param, NULL);
631 }
632 
633 static void wlan_led_set(struct led_classdev *led_cdev,
634 			 enum led_brightness value)
635 {
636 	struct asus_wmi *asus;
637 
638 	asus = container_of(led_cdev, struct asus_wmi, wlan_led);
639 
640 	asus->wlan_led_wk = !!value;
641 	queue_work(asus->led_workqueue, &asus->wlan_led_work);
642 }
643 
644 static enum led_brightness wlan_led_get(struct led_classdev *led_cdev)
645 {
646 	struct asus_wmi *asus;
647 	u32 result;
648 
649 	asus = container_of(led_cdev, struct asus_wmi, wlan_led);
650 	asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_WIRELESS_LED, &result);
651 
652 	return result & ASUS_WMI_DSTS_BRIGHTNESS_MASK;
653 }
654 
655 static void lightbar_led_update(struct work_struct *work)
656 {
657 	struct asus_wmi *asus;
658 	int ctrl_param;
659 
660 	asus = container_of(work, struct asus_wmi, lightbar_led_work);
661 
662 	ctrl_param = asus->lightbar_led_wk;
663 	asus_wmi_set_devstate(ASUS_WMI_DEVID_LIGHTBAR, ctrl_param, NULL);
664 }
665 
666 static void lightbar_led_set(struct led_classdev *led_cdev,
667 			     enum led_brightness value)
668 {
669 	struct asus_wmi *asus;
670 
671 	asus = container_of(led_cdev, struct asus_wmi, lightbar_led);
672 
673 	asus->lightbar_led_wk = !!value;
674 	queue_work(asus->led_workqueue, &asus->lightbar_led_work);
675 }
676 
677 static enum led_brightness lightbar_led_get(struct led_classdev *led_cdev)
678 {
679 	struct asus_wmi *asus;
680 	u32 result;
681 
682 	asus = container_of(led_cdev, struct asus_wmi, lightbar_led);
683 	asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_LIGHTBAR, &result);
684 
685 	return result & ASUS_WMI_DSTS_LIGHTBAR_MASK;
686 }
687 
688 static void asus_wmi_led_exit(struct asus_wmi *asus)
689 {
690 	led_classdev_unregister(&asus->kbd_led);
691 	led_classdev_unregister(&asus->tpd_led);
692 	led_classdev_unregister(&asus->wlan_led);
693 	led_classdev_unregister(&asus->lightbar_led);
694 
695 	if (asus->led_workqueue)
696 		destroy_workqueue(asus->led_workqueue);
697 }
698 
699 static int asus_wmi_led_init(struct asus_wmi *asus)
700 {
701 	int rv = 0, led_val;
702 
703 	asus->led_workqueue = create_singlethread_workqueue("led_workqueue");
704 	if (!asus->led_workqueue)
705 		return -ENOMEM;
706 
707 	if (read_tpd_led_state(asus) >= 0) {
708 		INIT_WORK(&asus->tpd_led_work, tpd_led_update);
709 
710 		asus->tpd_led.name = "asus::touchpad";
711 		asus->tpd_led.brightness_set = tpd_led_set;
712 		asus->tpd_led.brightness_get = tpd_led_get;
713 		asus->tpd_led.max_brightness = 1;
714 
715 		rv = led_classdev_register(&asus->platform_device->dev,
716 					   &asus->tpd_led);
717 		if (rv)
718 			goto error;
719 	}
720 
721 	if (!kbd_led_read(asus, &led_val, NULL)) {
722 		asus->kbd_led_wk = led_val;
723 		asus->kbd_led.name = "asus::kbd_backlight";
724 		asus->kbd_led.flags = LED_BRIGHT_HW_CHANGED;
725 		asus->kbd_led.brightness_set = kbd_led_set;
726 		asus->kbd_led.brightness_get = kbd_led_get;
727 		asus->kbd_led.max_brightness = 3;
728 
729 		rv = led_classdev_register(&asus->platform_device->dev,
730 					   &asus->kbd_led);
731 		if (rv)
732 			goto error;
733 	}
734 
735 	if (asus_wmi_dev_is_present(asus, ASUS_WMI_DEVID_WIRELESS_LED)
736 			&& (asus->driver->quirks->wapf > 0)) {
737 		INIT_WORK(&asus->wlan_led_work, wlan_led_update);
738 
739 		asus->wlan_led.name = "asus::wlan";
740 		asus->wlan_led.brightness_set = wlan_led_set;
741 		if (!wlan_led_unknown_state(asus))
742 			asus->wlan_led.brightness_get = wlan_led_get;
743 		asus->wlan_led.flags = LED_CORE_SUSPENDRESUME;
744 		asus->wlan_led.max_brightness = 1;
745 		asus->wlan_led.default_trigger = "asus-wlan";
746 
747 		rv = led_classdev_register(&asus->platform_device->dev,
748 					   &asus->wlan_led);
749 		if (rv)
750 			goto error;
751 	}
752 
753 	if (asus_wmi_dev_is_present(asus, ASUS_WMI_DEVID_LIGHTBAR)) {
754 		INIT_WORK(&asus->lightbar_led_work, lightbar_led_update);
755 
756 		asus->lightbar_led.name = "asus::lightbar";
757 		asus->lightbar_led.brightness_set = lightbar_led_set;
758 		asus->lightbar_led.brightness_get = lightbar_led_get;
759 		asus->lightbar_led.max_brightness = 1;
760 
761 		rv = led_classdev_register(&asus->platform_device->dev,
762 					   &asus->lightbar_led);
763 	}
764 
765 error:
766 	if (rv)
767 		asus_wmi_led_exit(asus);
768 
769 	return rv;
770 }
771 
772 /* RF *************************************************************************/
773 
774 /*
775  * PCI hotplug (for wlan rfkill)
776  */
777 static bool asus_wlan_rfkill_blocked(struct asus_wmi *asus)
778 {
779 	int result = asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_WLAN);
780 
781 	if (result < 0)
782 		return false;
783 	return !result;
784 }
785 
786 static void asus_rfkill_hotplug(struct asus_wmi *asus)
787 {
788 	struct pci_dev *dev;
789 	struct pci_bus *bus;
790 	bool blocked;
791 	bool absent;
792 	u32 l;
793 
794 	mutex_lock(&asus->wmi_lock);
795 	blocked = asus_wlan_rfkill_blocked(asus);
796 	mutex_unlock(&asus->wmi_lock);
797 
798 	mutex_lock(&asus->hotplug_lock);
799 	pci_lock_rescan_remove();
800 
801 	if (asus->wlan.rfkill)
802 		rfkill_set_sw_state(asus->wlan.rfkill, blocked);
803 
804 	if (asus->hotplug_slot.ops) {
805 		bus = pci_find_bus(0, 1);
806 		if (!bus) {
807 			pr_warn("Unable to find PCI bus 1?\n");
808 			goto out_unlock;
809 		}
810 
811 		if (pci_bus_read_config_dword(bus, 0, PCI_VENDOR_ID, &l)) {
812 			pr_err("Unable to read PCI config space?\n");
813 			goto out_unlock;
814 		}
815 		absent = (l == 0xffffffff);
816 
817 		if (blocked != absent) {
818 			pr_warn("BIOS says wireless lan is %s, "
819 				"but the pci device is %s\n",
820 				blocked ? "blocked" : "unblocked",
821 				absent ? "absent" : "present");
822 			pr_warn("skipped wireless hotplug as probably "
823 				"inappropriate for this model\n");
824 			goto out_unlock;
825 		}
826 
827 		if (!blocked) {
828 			dev = pci_get_slot(bus, 0);
829 			if (dev) {
830 				/* Device already present */
831 				pci_dev_put(dev);
832 				goto out_unlock;
833 			}
834 			dev = pci_scan_single_device(bus, 0);
835 			if (dev) {
836 				pci_bus_assign_resources(bus);
837 				pci_bus_add_device(dev);
838 			}
839 		} else {
840 			dev = pci_get_slot(bus, 0);
841 			if (dev) {
842 				pci_stop_and_remove_bus_device(dev);
843 				pci_dev_put(dev);
844 			}
845 		}
846 	}
847 
848 out_unlock:
849 	pci_unlock_rescan_remove();
850 	mutex_unlock(&asus->hotplug_lock);
851 }
852 
853 static void asus_rfkill_notify(acpi_handle handle, u32 event, void *data)
854 {
855 	struct asus_wmi *asus = data;
856 
857 	if (event != ACPI_NOTIFY_BUS_CHECK)
858 		return;
859 
860 	/*
861 	 * We can't call directly asus_rfkill_hotplug because most
862 	 * of the time WMBC is still being executed and not reetrant.
863 	 * There is currently no way to tell ACPICA that  we want this
864 	 * method to be serialized, we schedule a asus_rfkill_hotplug
865 	 * call later, in a safer context.
866 	 */
867 	queue_work(asus->hotplug_workqueue, &asus->hotplug_work);
868 }
869 
870 static int asus_register_rfkill_notifier(struct asus_wmi *asus, char *node)
871 {
872 	acpi_status status;
873 	acpi_handle handle;
874 
875 	status = acpi_get_handle(NULL, node, &handle);
876 	if (ACPI_FAILURE(status))
877 		return -ENODEV;
878 
879 	status = acpi_install_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
880 					     asus_rfkill_notify, asus);
881 	if (ACPI_FAILURE(status))
882 		pr_warn("Failed to register notify on %s\n", node);
883 
884 	return 0;
885 }
886 
887 static void asus_unregister_rfkill_notifier(struct asus_wmi *asus, char *node)
888 {
889 	acpi_status status = AE_OK;
890 	acpi_handle handle;
891 
892 	status = acpi_get_handle(NULL, node, &handle);
893 	if (ACPI_FAILURE(status))
894 		return;
895 
896 	status = acpi_remove_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
897 					    asus_rfkill_notify);
898 	if (ACPI_FAILURE(status))
899 		pr_err("Error removing rfkill notify handler %s\n", node);
900 }
901 
902 static int asus_get_adapter_status(struct hotplug_slot *hotplug_slot,
903 				   u8 *value)
904 {
905 	struct asus_wmi *asus = container_of(hotplug_slot,
906 					     struct asus_wmi, hotplug_slot);
907 	int result = asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_WLAN);
908 
909 	if (result < 0)
910 		return result;
911 
912 	*value = !!result;
913 	return 0;
914 }
915 
916 static const struct hotplug_slot_ops asus_hotplug_slot_ops = {
917 	.get_adapter_status = asus_get_adapter_status,
918 	.get_power_status = asus_get_adapter_status,
919 };
920 
921 static void asus_hotplug_work(struct work_struct *work)
922 {
923 	struct asus_wmi *asus;
924 
925 	asus = container_of(work, struct asus_wmi, hotplug_work);
926 	asus_rfkill_hotplug(asus);
927 }
928 
929 static int asus_setup_pci_hotplug(struct asus_wmi *asus)
930 {
931 	int ret = -ENOMEM;
932 	struct pci_bus *bus = pci_find_bus(0, 1);
933 
934 	if (!bus) {
935 		pr_err("Unable to find wifi PCI bus\n");
936 		return -ENODEV;
937 	}
938 
939 	asus->hotplug_workqueue =
940 	    create_singlethread_workqueue("hotplug_workqueue");
941 	if (!asus->hotplug_workqueue)
942 		goto error_workqueue;
943 
944 	INIT_WORK(&asus->hotplug_work, asus_hotplug_work);
945 
946 	asus->hotplug_slot.ops = &asus_hotplug_slot_ops;
947 
948 	ret = pci_hp_register(&asus->hotplug_slot, bus, 0, "asus-wifi");
949 	if (ret) {
950 		pr_err("Unable to register hotplug slot - %d\n", ret);
951 		goto error_register;
952 	}
953 
954 	return 0;
955 
956 error_register:
957 	asus->hotplug_slot.ops = NULL;
958 	destroy_workqueue(asus->hotplug_workqueue);
959 error_workqueue:
960 	return ret;
961 }
962 
963 /*
964  * Rfkill devices
965  */
966 static int asus_rfkill_set(void *data, bool blocked)
967 {
968 	struct asus_rfkill *priv = data;
969 	u32 ctrl_param = !blocked;
970 	u32 dev_id = priv->dev_id;
971 
972 	/*
973 	 * If the user bit is set, BIOS can't set and record the wlan status,
974 	 * it will report the value read from id ASUS_WMI_DEVID_WLAN_LED
975 	 * while we query the wlan status through WMI(ASUS_WMI_DEVID_WLAN).
976 	 * So, we have to record wlan status in id ASUS_WMI_DEVID_WLAN_LED
977 	 * while setting the wlan status through WMI.
978 	 * This is also the behavior that windows app will do.
979 	 */
980 	if ((dev_id == ASUS_WMI_DEVID_WLAN) &&
981 	     priv->asus->driver->wlan_ctrl_by_user)
982 		dev_id = ASUS_WMI_DEVID_WLAN_LED;
983 
984 	return asus_wmi_set_devstate(dev_id, ctrl_param, NULL);
985 }
986 
987 static void asus_rfkill_query(struct rfkill *rfkill, void *data)
988 {
989 	struct asus_rfkill *priv = data;
990 	int result;
991 
992 	result = asus_wmi_get_devstate_simple(priv->asus, priv->dev_id);
993 
994 	if (result < 0)
995 		return;
996 
997 	rfkill_set_sw_state(priv->rfkill, !result);
998 }
999 
1000 static int asus_rfkill_wlan_set(void *data, bool blocked)
1001 {
1002 	struct asus_rfkill *priv = data;
1003 	struct asus_wmi *asus = priv->asus;
1004 	int ret;
1005 
1006 	/*
1007 	 * This handler is enabled only if hotplug is enabled.
1008 	 * In this case, the asus_wmi_set_devstate() will
1009 	 * trigger a wmi notification and we need to wait
1010 	 * this call to finish before being able to call
1011 	 * any wmi method
1012 	 */
1013 	mutex_lock(&asus->wmi_lock);
1014 	ret = asus_rfkill_set(data, blocked);
1015 	mutex_unlock(&asus->wmi_lock);
1016 	return ret;
1017 }
1018 
1019 static const struct rfkill_ops asus_rfkill_wlan_ops = {
1020 	.set_block = asus_rfkill_wlan_set,
1021 	.query = asus_rfkill_query,
1022 };
1023 
1024 static const struct rfkill_ops asus_rfkill_ops = {
1025 	.set_block = asus_rfkill_set,
1026 	.query = asus_rfkill_query,
1027 };
1028 
1029 static int asus_new_rfkill(struct asus_wmi *asus,
1030 			   struct asus_rfkill *arfkill,
1031 			   const char *name, enum rfkill_type type, int dev_id)
1032 {
1033 	int result = asus_wmi_get_devstate_simple(asus, dev_id);
1034 	struct rfkill **rfkill = &arfkill->rfkill;
1035 
1036 	if (result < 0)
1037 		return result;
1038 
1039 	arfkill->dev_id = dev_id;
1040 	arfkill->asus = asus;
1041 
1042 	if (dev_id == ASUS_WMI_DEVID_WLAN &&
1043 	    asus->driver->quirks->hotplug_wireless)
1044 		*rfkill = rfkill_alloc(name, &asus->platform_device->dev, type,
1045 				       &asus_rfkill_wlan_ops, arfkill);
1046 	else
1047 		*rfkill = rfkill_alloc(name, &asus->platform_device->dev, type,
1048 				       &asus_rfkill_ops, arfkill);
1049 
1050 	if (!*rfkill)
1051 		return -EINVAL;
1052 
1053 	if ((dev_id == ASUS_WMI_DEVID_WLAN) &&
1054 			(asus->driver->quirks->wapf > 0))
1055 		rfkill_set_led_trigger_name(*rfkill, "asus-wlan");
1056 
1057 	rfkill_init_sw_state(*rfkill, !result);
1058 	result = rfkill_register(*rfkill);
1059 	if (result) {
1060 		rfkill_destroy(*rfkill);
1061 		*rfkill = NULL;
1062 		return result;
1063 	}
1064 	return 0;
1065 }
1066 
1067 static void asus_wmi_rfkill_exit(struct asus_wmi *asus)
1068 {
1069 	if (asus->driver->wlan_ctrl_by_user && ashs_present())
1070 		return;
1071 
1072 	asus_unregister_rfkill_notifier(asus, "\\_SB.PCI0.P0P5");
1073 	asus_unregister_rfkill_notifier(asus, "\\_SB.PCI0.P0P6");
1074 	asus_unregister_rfkill_notifier(asus, "\\_SB.PCI0.P0P7");
1075 	if (asus->wlan.rfkill) {
1076 		rfkill_unregister(asus->wlan.rfkill);
1077 		rfkill_destroy(asus->wlan.rfkill);
1078 		asus->wlan.rfkill = NULL;
1079 	}
1080 	/*
1081 	 * Refresh pci hotplug in case the rfkill state was changed after
1082 	 * asus_unregister_rfkill_notifier()
1083 	 */
1084 	asus_rfkill_hotplug(asus);
1085 	if (asus->hotplug_slot.ops)
1086 		pci_hp_deregister(&asus->hotplug_slot);
1087 	if (asus->hotplug_workqueue)
1088 		destroy_workqueue(asus->hotplug_workqueue);
1089 
1090 	if (asus->bluetooth.rfkill) {
1091 		rfkill_unregister(asus->bluetooth.rfkill);
1092 		rfkill_destroy(asus->bluetooth.rfkill);
1093 		asus->bluetooth.rfkill = NULL;
1094 	}
1095 	if (asus->wimax.rfkill) {
1096 		rfkill_unregister(asus->wimax.rfkill);
1097 		rfkill_destroy(asus->wimax.rfkill);
1098 		asus->wimax.rfkill = NULL;
1099 	}
1100 	if (asus->wwan3g.rfkill) {
1101 		rfkill_unregister(asus->wwan3g.rfkill);
1102 		rfkill_destroy(asus->wwan3g.rfkill);
1103 		asus->wwan3g.rfkill = NULL;
1104 	}
1105 	if (asus->gps.rfkill) {
1106 		rfkill_unregister(asus->gps.rfkill);
1107 		rfkill_destroy(asus->gps.rfkill);
1108 		asus->gps.rfkill = NULL;
1109 	}
1110 	if (asus->uwb.rfkill) {
1111 		rfkill_unregister(asus->uwb.rfkill);
1112 		rfkill_destroy(asus->uwb.rfkill);
1113 		asus->uwb.rfkill = NULL;
1114 	}
1115 }
1116 
1117 static int asus_wmi_rfkill_init(struct asus_wmi *asus)
1118 {
1119 	int result = 0;
1120 
1121 	mutex_init(&asus->hotplug_lock);
1122 	mutex_init(&asus->wmi_lock);
1123 
1124 	result = asus_new_rfkill(asus, &asus->wlan, "asus-wlan",
1125 				 RFKILL_TYPE_WLAN, ASUS_WMI_DEVID_WLAN);
1126 
1127 	if (result && result != -ENODEV)
1128 		goto exit;
1129 
1130 	result = asus_new_rfkill(asus, &asus->bluetooth,
1131 				 "asus-bluetooth", RFKILL_TYPE_BLUETOOTH,
1132 				 ASUS_WMI_DEVID_BLUETOOTH);
1133 
1134 	if (result && result != -ENODEV)
1135 		goto exit;
1136 
1137 	result = asus_new_rfkill(asus, &asus->wimax, "asus-wimax",
1138 				 RFKILL_TYPE_WIMAX, ASUS_WMI_DEVID_WIMAX);
1139 
1140 	if (result && result != -ENODEV)
1141 		goto exit;
1142 
1143 	result = asus_new_rfkill(asus, &asus->wwan3g, "asus-wwan3g",
1144 				 RFKILL_TYPE_WWAN, ASUS_WMI_DEVID_WWAN3G);
1145 
1146 	if (result && result != -ENODEV)
1147 		goto exit;
1148 
1149 	result = asus_new_rfkill(asus, &asus->gps, "asus-gps",
1150 				 RFKILL_TYPE_GPS, ASUS_WMI_DEVID_GPS);
1151 
1152 	if (result && result != -ENODEV)
1153 		goto exit;
1154 
1155 	result = asus_new_rfkill(asus, &asus->uwb, "asus-uwb",
1156 				 RFKILL_TYPE_UWB, ASUS_WMI_DEVID_UWB);
1157 
1158 	if (result && result != -ENODEV)
1159 		goto exit;
1160 
1161 	if (!asus->driver->quirks->hotplug_wireless)
1162 		goto exit;
1163 
1164 	result = asus_setup_pci_hotplug(asus);
1165 	/*
1166 	 * If we get -EBUSY then something else is handling the PCI hotplug -
1167 	 * don't fail in this case
1168 	 */
1169 	if (result == -EBUSY)
1170 		result = 0;
1171 
1172 	asus_register_rfkill_notifier(asus, "\\_SB.PCI0.P0P5");
1173 	asus_register_rfkill_notifier(asus, "\\_SB.PCI0.P0P6");
1174 	asus_register_rfkill_notifier(asus, "\\_SB.PCI0.P0P7");
1175 	/*
1176 	 * Refresh pci hotplug in case the rfkill state was changed during
1177 	 * setup.
1178 	 */
1179 	asus_rfkill_hotplug(asus);
1180 
1181 exit:
1182 	if (result && result != -ENODEV)
1183 		asus_wmi_rfkill_exit(asus);
1184 
1185 	if (result == -ENODEV)
1186 		result = 0;
1187 
1188 	return result;
1189 }
1190 
1191 /* Quirks *********************************************************************/
1192 
1193 static void asus_wmi_set_xusb2pr(struct asus_wmi *asus)
1194 {
1195 	struct pci_dev *xhci_pdev;
1196 	u32 orig_ports_available;
1197 	u32 ports_available = asus->driver->quirks->xusb2pr;
1198 
1199 	xhci_pdev = pci_get_device(PCI_VENDOR_ID_INTEL,
1200 			PCI_DEVICE_ID_INTEL_LYNXPOINT_LP_XHCI,
1201 			NULL);
1202 
1203 	if (!xhci_pdev)
1204 		return;
1205 
1206 	pci_read_config_dword(xhci_pdev, USB_INTEL_XUSB2PR,
1207 				&orig_ports_available);
1208 
1209 	pci_write_config_dword(xhci_pdev, USB_INTEL_XUSB2PR,
1210 				cpu_to_le32(ports_available));
1211 
1212 	pr_info("set USB_INTEL_XUSB2PR old: 0x%04x, new: 0x%04x\n",
1213 			orig_ports_available, ports_available);
1214 }
1215 
1216 /*
1217  * Some devices dont support or have borcken get_als method
1218  * but still support set method.
1219  */
1220 static void asus_wmi_set_als(void)
1221 {
1222 	asus_wmi_set_devstate(ASUS_WMI_DEVID_ALS_ENABLE, 1, NULL);
1223 }
1224 
1225 /* Hwmon device ***************************************************************/
1226 
1227 static int asus_agfn_fan_speed_read(struct asus_wmi *asus, int fan,
1228 					  int *speed)
1229 {
1230 	struct agfn_fan_args args = {
1231 		.agfn.len = sizeof(args),
1232 		.agfn.mfun = ASUS_FAN_MFUN,
1233 		.agfn.sfun = ASUS_FAN_SFUN_READ,
1234 		.fan = fan,
1235 		.speed = 0,
1236 	};
1237 	struct acpi_buffer input = { (acpi_size) sizeof(args), &args };
1238 	int status;
1239 
1240 	if (fan != 1)
1241 		return -EINVAL;
1242 
1243 	status = asus_wmi_evaluate_method_agfn(input);
1244 
1245 	if (status || args.agfn.err)
1246 		return -ENXIO;
1247 
1248 	if (speed)
1249 		*speed = args.speed;
1250 
1251 	return 0;
1252 }
1253 
1254 static int asus_agfn_fan_speed_write(struct asus_wmi *asus, int fan,
1255 				     int *speed)
1256 {
1257 	struct agfn_fan_args args = {
1258 		.agfn.len = sizeof(args),
1259 		.agfn.mfun = ASUS_FAN_MFUN,
1260 		.agfn.sfun = ASUS_FAN_SFUN_WRITE,
1261 		.fan = fan,
1262 		.speed = speed ?  *speed : 0,
1263 	};
1264 	struct acpi_buffer input = { (acpi_size) sizeof(args), &args };
1265 	int status;
1266 
1267 	/* 1: for setting 1st fan's speed 0: setting auto mode */
1268 	if (fan != 1 && fan != 0)
1269 		return -EINVAL;
1270 
1271 	status = asus_wmi_evaluate_method_agfn(input);
1272 
1273 	if (status || args.agfn.err)
1274 		return -ENXIO;
1275 
1276 	if (speed && fan == 1)
1277 		asus->agfn_pwm = *speed;
1278 
1279 	return 0;
1280 }
1281 
1282 /*
1283  * Check if we can read the speed of one fan. If true we assume we can also
1284  * control it.
1285  */
1286 static bool asus_wmi_has_agfn_fan(struct asus_wmi *asus)
1287 {
1288 	int status;
1289 	int speed;
1290 	u32 value;
1291 
1292 	status = asus_agfn_fan_speed_read(asus, 1, &speed);
1293 	if (status != 0)
1294 		return false;
1295 
1296 	status = asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_FAN_CTRL, &value);
1297 	if (status != 0)
1298 		return false;
1299 
1300 	/*
1301 	 * We need to find a better way, probably using sfun,
1302 	 * bits or spec ...
1303 	 * Currently we disable it if:
1304 	 * - ASUS_WMI_UNSUPPORTED_METHOD is returned
1305 	 * - reverved bits are non-zero
1306 	 * - sfun and presence bit are not set
1307 	 */
1308 	return !(value == ASUS_WMI_UNSUPPORTED_METHOD || value & 0xFFF80000
1309 		 || (!asus->sfun && !(value & ASUS_WMI_DSTS_PRESENCE_BIT)));
1310 }
1311 
1312 static int asus_fan_set_auto(struct asus_wmi *asus)
1313 {
1314 	int status;
1315 	u32 retval;
1316 
1317 	switch (asus->fan_type) {
1318 	case FAN_TYPE_SPEC83:
1319 		status = asus_wmi_set_devstate(ASUS_WMI_DEVID_CPU_FAN_CTRL,
1320 					       0, &retval);
1321 		if (status)
1322 			return status;
1323 
1324 		if (retval != 1)
1325 			return -EIO;
1326 		break;
1327 
1328 	case FAN_TYPE_AGFN:
1329 		status = asus_agfn_fan_speed_write(asus, 0, NULL);
1330 		if (status)
1331 			return -ENXIO;
1332 		break;
1333 
1334 	default:
1335 		return -ENXIO;
1336 	}
1337 
1338 
1339 	return 0;
1340 }
1341 
1342 static ssize_t pwm1_show(struct device *dev,
1343 			       struct device_attribute *attr,
1344 			       char *buf)
1345 {
1346 	struct asus_wmi *asus = dev_get_drvdata(dev);
1347 	int err;
1348 	int value;
1349 
1350 	/* If we already set a value then just return it */
1351 	if (asus->agfn_pwm >= 0)
1352 		return sprintf(buf, "%d\n", asus->agfn_pwm);
1353 
1354 	/*
1355 	 * If we haven't set already set a value through the AGFN interface,
1356 	 * we read a current value through the (now-deprecated) FAN_CTRL device.
1357 	 */
1358 	err = asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_FAN_CTRL, &value);
1359 	if (err < 0)
1360 		return err;
1361 
1362 	value &= 0xFF;
1363 
1364 	if (value == 1) /* Low Speed */
1365 		value = 85;
1366 	else if (value == 2)
1367 		value = 170;
1368 	else if (value == 3)
1369 		value = 255;
1370 	else if (value) {
1371 		pr_err("Unknown fan speed %#x\n", value);
1372 		value = -1;
1373 	}
1374 
1375 	return sprintf(buf, "%d\n", value);
1376 }
1377 
1378 static ssize_t pwm1_store(struct device *dev,
1379 				     struct device_attribute *attr,
1380 				     const char *buf, size_t count) {
1381 	struct asus_wmi *asus = dev_get_drvdata(dev);
1382 	int value;
1383 	int state;
1384 	int ret;
1385 
1386 	ret = kstrtouint(buf, 10, &value);
1387 	if (ret)
1388 		return ret;
1389 
1390 	value = clamp(value, 0, 255);
1391 
1392 	state = asus_agfn_fan_speed_write(asus, 1, &value);
1393 	if (state)
1394 		pr_warn("Setting fan speed failed: %d\n", state);
1395 	else
1396 		asus->fan_pwm_mode = ASUS_FAN_CTRL_MANUAL;
1397 
1398 	return count;
1399 }
1400 
1401 static ssize_t fan1_input_show(struct device *dev,
1402 					struct device_attribute *attr,
1403 					char *buf)
1404 {
1405 	struct asus_wmi *asus = dev_get_drvdata(dev);
1406 	int value;
1407 	int ret;
1408 
1409 	switch (asus->fan_type) {
1410 	case FAN_TYPE_SPEC83:
1411 		ret = asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_CPU_FAN_CTRL,
1412 					    &value);
1413 		if (ret < 0)
1414 			return ret;
1415 
1416 		value &= 0xffff;
1417 		break;
1418 
1419 	case FAN_TYPE_AGFN:
1420 		/* no speed readable on manual mode */
1421 		if (asus->fan_pwm_mode == ASUS_FAN_CTRL_MANUAL)
1422 			return -ENXIO;
1423 
1424 		ret = asus_agfn_fan_speed_read(asus, 1, &value);
1425 		if (ret) {
1426 			pr_warn("reading fan speed failed: %d\n", ret);
1427 			return -ENXIO;
1428 		}
1429 		break;
1430 
1431 	default:
1432 		return -ENXIO;
1433 	}
1434 
1435 	return sprintf(buf, "%d\n", value < 0 ? -1 : value*100);
1436 }
1437 
1438 static ssize_t pwm1_enable_show(struct device *dev,
1439 						 struct device_attribute *attr,
1440 						 char *buf)
1441 {
1442 	struct asus_wmi *asus = dev_get_drvdata(dev);
1443 
1444 	/*
1445 	 * Just read back the cached pwm mode.
1446 	 *
1447 	 * For the CPU_FAN device, the spec indicates that we should be
1448 	 * able to read the device status and consult bit 19 to see if we
1449 	 * are in Full On or Automatic mode. However, this does not work
1450 	 * in practice on X532FL at least (the bit is always 0) and there's
1451 	 * also nothing in the DSDT to indicate that this behaviour exists.
1452 	 */
1453 	return sprintf(buf, "%d\n", asus->fan_pwm_mode);
1454 }
1455 
1456 static ssize_t pwm1_enable_store(struct device *dev,
1457 						  struct device_attribute *attr,
1458 						  const char *buf, size_t count)
1459 {
1460 	struct asus_wmi *asus = dev_get_drvdata(dev);
1461 	int status = 0;
1462 	int state;
1463 	int value;
1464 	int ret;
1465 	u32 retval;
1466 
1467 	ret = kstrtouint(buf, 10, &state);
1468 	if (ret)
1469 		return ret;
1470 
1471 	if (asus->fan_type == FAN_TYPE_SPEC83) {
1472 		switch (state) { /* standard documented hwmon values */
1473 		case ASUS_FAN_CTRL_FULLSPEED:
1474 			value = 1;
1475 			break;
1476 		case ASUS_FAN_CTRL_AUTO:
1477 			value = 0;
1478 			break;
1479 		default:
1480 			return -EINVAL;
1481 		}
1482 
1483 		ret = asus_wmi_set_devstate(ASUS_WMI_DEVID_CPU_FAN_CTRL,
1484 					    value, &retval);
1485 		if (ret)
1486 			return ret;
1487 
1488 		if (retval != 1)
1489 			return -EIO;
1490 	} else if (asus->fan_type == FAN_TYPE_AGFN) {
1491 		switch (state) {
1492 		case ASUS_FAN_CTRL_MANUAL:
1493 			break;
1494 
1495 		case ASUS_FAN_CTRL_AUTO:
1496 			status = asus_fan_set_auto(asus);
1497 			if (status)
1498 				return status;
1499 			break;
1500 
1501 		default:
1502 			return -EINVAL;
1503 		}
1504 	}
1505 
1506 	asus->fan_pwm_mode = state;
1507 	return count;
1508 }
1509 
1510 static ssize_t fan1_label_show(struct device *dev,
1511 					  struct device_attribute *attr,
1512 					  char *buf)
1513 {
1514 	return sprintf(buf, "%s\n", ASUS_FAN_DESC);
1515 }
1516 
1517 static ssize_t asus_hwmon_temp1(struct device *dev,
1518 				struct device_attribute *attr,
1519 				char *buf)
1520 {
1521 	struct asus_wmi *asus = dev_get_drvdata(dev);
1522 	u32 value;
1523 	int err;
1524 
1525 	err = asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_THERMAL_CTRL, &value);
1526 	if (err < 0)
1527 		return err;
1528 
1529 	return sprintf(buf, "%ld\n",
1530 		       deci_kelvin_to_millicelsius(value & 0xFFFF));
1531 }
1532 
1533 /* Fan1 */
1534 static DEVICE_ATTR_RW(pwm1);
1535 static DEVICE_ATTR_RW(pwm1_enable);
1536 static DEVICE_ATTR_RO(fan1_input);
1537 static DEVICE_ATTR_RO(fan1_label);
1538 
1539 /* Temperature */
1540 static DEVICE_ATTR(temp1_input, S_IRUGO, asus_hwmon_temp1, NULL);
1541 
1542 static struct attribute *hwmon_attributes[] = {
1543 	&dev_attr_pwm1.attr,
1544 	&dev_attr_pwm1_enable.attr,
1545 	&dev_attr_fan1_input.attr,
1546 	&dev_attr_fan1_label.attr,
1547 
1548 	&dev_attr_temp1_input.attr,
1549 	NULL
1550 };
1551 
1552 static umode_t asus_hwmon_sysfs_is_visible(struct kobject *kobj,
1553 					  struct attribute *attr, int idx)
1554 {
1555 	struct device *dev = container_of(kobj, struct device, kobj);
1556 	struct asus_wmi *asus = dev_get_drvdata(dev->parent);
1557 	u32 value = ASUS_WMI_UNSUPPORTED_METHOD;
1558 
1559 	if (attr == &dev_attr_pwm1.attr) {
1560 		if (asus->fan_type != FAN_TYPE_AGFN)
1561 			return 0;
1562 	} else if (attr == &dev_attr_fan1_input.attr
1563 	    || attr == &dev_attr_fan1_label.attr
1564 	    || attr == &dev_attr_pwm1_enable.attr) {
1565 		if (asus->fan_type == FAN_TYPE_NONE)
1566 			return 0;
1567 	} else if (attr == &dev_attr_temp1_input.attr) {
1568 		int err = asus_wmi_get_devstate(asus,
1569 						ASUS_WMI_DEVID_THERMAL_CTRL,
1570 						&value);
1571 
1572 		if (err < 0)
1573 			return 0; /* can't return negative here */
1574 
1575 		/*
1576 		 * If the temperature value in deci-Kelvin is near the absolute
1577 		 * zero temperature, something is clearly wrong
1578 		 */
1579 		if (value == 0 || value == 1)
1580 			return 0;
1581 	}
1582 
1583 	return attr->mode;
1584 }
1585 
1586 static const struct attribute_group hwmon_attribute_group = {
1587 	.is_visible = asus_hwmon_sysfs_is_visible,
1588 	.attrs = hwmon_attributes
1589 };
1590 __ATTRIBUTE_GROUPS(hwmon_attribute);
1591 
1592 static int asus_wmi_hwmon_init(struct asus_wmi *asus)
1593 {
1594 	struct device *dev = &asus->platform_device->dev;
1595 	struct device *hwmon;
1596 
1597 	hwmon = devm_hwmon_device_register_with_groups(dev, "asus", asus,
1598 			hwmon_attribute_groups);
1599 
1600 	if (IS_ERR(hwmon)) {
1601 		pr_err("Could not register asus hwmon device\n");
1602 		return PTR_ERR(hwmon);
1603 	}
1604 	return 0;
1605 }
1606 
1607 static int asus_wmi_fan_init(struct asus_wmi *asus)
1608 {
1609 	asus->fan_type = FAN_TYPE_NONE;
1610 	asus->agfn_pwm = -1;
1611 
1612 	if (asus_wmi_dev_is_present(asus, ASUS_WMI_DEVID_CPU_FAN_CTRL))
1613 		asus->fan_type = FAN_TYPE_SPEC83;
1614 	else if (asus_wmi_has_agfn_fan(asus))
1615 		asus->fan_type = FAN_TYPE_AGFN;
1616 
1617 	if (asus->fan_type == FAN_TYPE_NONE)
1618 		return -ENODEV;
1619 
1620 	asus_fan_set_auto(asus);
1621 	asus->fan_pwm_mode = ASUS_FAN_CTRL_AUTO;
1622 	return 0;
1623 }
1624 
1625 /* Fan mode *******************************************************************/
1626 
1627 static int fan_boost_mode_check_present(struct asus_wmi *asus)
1628 {
1629 	u32 result;
1630 	int err;
1631 
1632 	asus->fan_boost_mode_available = false;
1633 
1634 	err = asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_FAN_BOOST_MODE,
1635 				    &result);
1636 	if (err) {
1637 		if (err == -ENODEV)
1638 			return 0;
1639 		else
1640 			return err;
1641 	}
1642 
1643 	if ((result & ASUS_WMI_DSTS_PRESENCE_BIT) &&
1644 			(result & ASUS_FAN_BOOST_MODES_MASK)) {
1645 		asus->fan_boost_mode_available = true;
1646 		asus->fan_boost_mode_mask = result & ASUS_FAN_BOOST_MODES_MASK;
1647 	}
1648 
1649 	return 0;
1650 }
1651 
1652 static int fan_boost_mode_write(struct asus_wmi *asus)
1653 {
1654 	int err;
1655 	u8 value;
1656 	u32 retval;
1657 
1658 	value = asus->fan_boost_mode;
1659 
1660 	pr_info("Set fan boost mode: %u\n", value);
1661 	err = asus_wmi_set_devstate(ASUS_WMI_DEVID_FAN_BOOST_MODE, value,
1662 				    &retval);
1663 	if (err) {
1664 		pr_warn("Failed to set fan boost mode: %d\n", err);
1665 		return err;
1666 	}
1667 
1668 	if (retval != 1) {
1669 		pr_warn("Failed to set fan boost mode (retval): 0x%x\n",
1670 			retval);
1671 		return -EIO;
1672 	}
1673 
1674 	return 0;
1675 }
1676 
1677 static int fan_boost_mode_switch_next(struct asus_wmi *asus)
1678 {
1679 	u8 mask = asus->fan_boost_mode_mask;
1680 
1681 	if (asus->fan_boost_mode == ASUS_FAN_BOOST_MODE_NORMAL) {
1682 		if (mask & ASUS_FAN_BOOST_MODE_OVERBOOST_MASK)
1683 			asus->fan_boost_mode = ASUS_FAN_BOOST_MODE_OVERBOOST;
1684 		else if (mask & ASUS_FAN_BOOST_MODE_SILENT_MASK)
1685 			asus->fan_boost_mode = ASUS_FAN_BOOST_MODE_SILENT;
1686 	} else if (asus->fan_boost_mode == ASUS_FAN_BOOST_MODE_OVERBOOST) {
1687 		if (mask & ASUS_FAN_BOOST_MODE_SILENT_MASK)
1688 			asus->fan_boost_mode = ASUS_FAN_BOOST_MODE_SILENT;
1689 		else
1690 			asus->fan_boost_mode = ASUS_FAN_BOOST_MODE_NORMAL;
1691 	} else {
1692 		asus->fan_boost_mode = ASUS_FAN_BOOST_MODE_NORMAL;
1693 	}
1694 
1695 	return fan_boost_mode_write(asus);
1696 }
1697 
1698 static ssize_t fan_boost_mode_show(struct device *dev,
1699 				   struct device_attribute *attr, char *buf)
1700 {
1701 	struct asus_wmi *asus = dev_get_drvdata(dev);
1702 
1703 	return scnprintf(buf, PAGE_SIZE, "%d\n", asus->fan_boost_mode);
1704 }
1705 
1706 static ssize_t fan_boost_mode_store(struct device *dev,
1707 				    struct device_attribute *attr,
1708 				    const char *buf, size_t count)
1709 {
1710 	int result;
1711 	u8 new_mode;
1712 	struct asus_wmi *asus = dev_get_drvdata(dev);
1713 	u8 mask = asus->fan_boost_mode_mask;
1714 
1715 	result = kstrtou8(buf, 10, &new_mode);
1716 	if (result < 0) {
1717 		pr_warn("Trying to store invalid value\n");
1718 		return result;
1719 	}
1720 
1721 	if (new_mode == ASUS_FAN_BOOST_MODE_OVERBOOST) {
1722 		if (!(mask & ASUS_FAN_BOOST_MODE_OVERBOOST_MASK))
1723 			return -EINVAL;
1724 	} else if (new_mode == ASUS_FAN_BOOST_MODE_SILENT) {
1725 		if (!(mask & ASUS_FAN_BOOST_MODE_SILENT_MASK))
1726 			return -EINVAL;
1727 	} else if (new_mode != ASUS_FAN_BOOST_MODE_NORMAL) {
1728 		return -EINVAL;
1729 	}
1730 
1731 	asus->fan_boost_mode = new_mode;
1732 	fan_boost_mode_write(asus);
1733 
1734 	return count;
1735 }
1736 
1737 // Fan boost mode: 0 - normal, 1 - overboost, 2 - silent
1738 static DEVICE_ATTR_RW(fan_boost_mode);
1739 
1740 /* Throttle thermal policy ****************************************************/
1741 
1742 static int throttle_thermal_policy_check_present(struct asus_wmi *asus)
1743 {
1744 	u32 result;
1745 	int err;
1746 
1747 	asus->throttle_thermal_policy_available = false;
1748 
1749 	err = asus_wmi_get_devstate(asus,
1750 				    ASUS_WMI_DEVID_THROTTLE_THERMAL_POLICY,
1751 				    &result);
1752 	if (err) {
1753 		if (err == -ENODEV)
1754 			return 0;
1755 		return err;
1756 	}
1757 
1758 	if (result & ASUS_WMI_DSTS_PRESENCE_BIT)
1759 		asus->throttle_thermal_policy_available = true;
1760 
1761 	return 0;
1762 }
1763 
1764 static int throttle_thermal_policy_write(struct asus_wmi *asus)
1765 {
1766 	int err;
1767 	u8 value;
1768 	u32 retval;
1769 
1770 	value = asus->throttle_thermal_policy_mode;
1771 
1772 	err = asus_wmi_set_devstate(ASUS_WMI_DEVID_THROTTLE_THERMAL_POLICY,
1773 				    value, &retval);
1774 	if (err) {
1775 		pr_warn("Failed to set throttle thermal policy: %d\n", err);
1776 		return err;
1777 	}
1778 
1779 	if (retval != 1) {
1780 		pr_warn("Failed to set throttle thermal policy (retval): 0x%x\n",
1781 			retval);
1782 		return -EIO;
1783 	}
1784 
1785 	return 0;
1786 }
1787 
1788 static int throttle_thermal_policy_set_default(struct asus_wmi *asus)
1789 {
1790 	if (!asus->throttle_thermal_policy_available)
1791 		return 0;
1792 
1793 	asus->throttle_thermal_policy_mode = ASUS_THROTTLE_THERMAL_POLICY_DEFAULT;
1794 	return throttle_thermal_policy_write(asus);
1795 }
1796 
1797 static int throttle_thermal_policy_switch_next(struct asus_wmi *asus)
1798 {
1799 	u8 new_mode = asus->throttle_thermal_policy_mode + 1;
1800 
1801 	if (new_mode > ASUS_THROTTLE_THERMAL_POLICY_SILENT)
1802 		new_mode = ASUS_THROTTLE_THERMAL_POLICY_DEFAULT;
1803 
1804 	asus->throttle_thermal_policy_mode = new_mode;
1805 	return throttle_thermal_policy_write(asus);
1806 }
1807 
1808 static ssize_t throttle_thermal_policy_show(struct device *dev,
1809 				   struct device_attribute *attr, char *buf)
1810 {
1811 	struct asus_wmi *asus = dev_get_drvdata(dev);
1812 	u8 mode = asus->throttle_thermal_policy_mode;
1813 
1814 	return scnprintf(buf, PAGE_SIZE, "%d\n", mode);
1815 }
1816 
1817 static ssize_t throttle_thermal_policy_store(struct device *dev,
1818 				    struct device_attribute *attr,
1819 				    const char *buf, size_t count)
1820 {
1821 	int result;
1822 	u8 new_mode;
1823 	struct asus_wmi *asus = dev_get_drvdata(dev);
1824 
1825 	result = kstrtou8(buf, 10, &new_mode);
1826 	if (result < 0)
1827 		return result;
1828 
1829 	if (new_mode > ASUS_THROTTLE_THERMAL_POLICY_SILENT)
1830 		return -EINVAL;
1831 
1832 	asus->throttle_thermal_policy_mode = new_mode;
1833 	throttle_thermal_policy_write(asus);
1834 
1835 	return count;
1836 }
1837 
1838 // Throttle thermal policy: 0 - default, 1 - overboost, 2 - silent
1839 static DEVICE_ATTR_RW(throttle_thermal_policy);
1840 
1841 /* Backlight ******************************************************************/
1842 
1843 static int read_backlight_power(struct asus_wmi *asus)
1844 {
1845 	int ret;
1846 
1847 	if (asus->driver->quirks->store_backlight_power)
1848 		ret = !asus->driver->panel_power;
1849 	else
1850 		ret = asus_wmi_get_devstate_simple(asus,
1851 						   ASUS_WMI_DEVID_BACKLIGHT);
1852 
1853 	if (ret < 0)
1854 		return ret;
1855 
1856 	return ret ? FB_BLANK_UNBLANK : FB_BLANK_POWERDOWN;
1857 }
1858 
1859 static int read_brightness_max(struct asus_wmi *asus)
1860 {
1861 	u32 retval;
1862 	int err;
1863 
1864 	err = asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_BRIGHTNESS, &retval);
1865 	if (err < 0)
1866 		return err;
1867 
1868 	retval = retval & ASUS_WMI_DSTS_MAX_BRIGTH_MASK;
1869 	retval >>= 8;
1870 
1871 	if (!retval)
1872 		return -ENODEV;
1873 
1874 	return retval;
1875 }
1876 
1877 static int read_brightness(struct backlight_device *bd)
1878 {
1879 	struct asus_wmi *asus = bl_get_data(bd);
1880 	u32 retval;
1881 	int err;
1882 
1883 	err = asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_BRIGHTNESS, &retval);
1884 	if (err < 0)
1885 		return err;
1886 
1887 	return retval & ASUS_WMI_DSTS_BRIGHTNESS_MASK;
1888 }
1889 
1890 static u32 get_scalar_command(struct backlight_device *bd)
1891 {
1892 	struct asus_wmi *asus = bl_get_data(bd);
1893 	u32 ctrl_param = 0;
1894 
1895 	if ((asus->driver->brightness < bd->props.brightness) ||
1896 	    bd->props.brightness == bd->props.max_brightness)
1897 		ctrl_param = 0x00008001;
1898 	else if ((asus->driver->brightness > bd->props.brightness) ||
1899 		 bd->props.brightness == 0)
1900 		ctrl_param = 0x00008000;
1901 
1902 	asus->driver->brightness = bd->props.brightness;
1903 
1904 	return ctrl_param;
1905 }
1906 
1907 static int update_bl_status(struct backlight_device *bd)
1908 {
1909 	struct asus_wmi *asus = bl_get_data(bd);
1910 	u32 ctrl_param;
1911 	int power, err = 0;
1912 
1913 	power = read_backlight_power(asus);
1914 	if (power != -ENODEV && bd->props.power != power) {
1915 		ctrl_param = !!(bd->props.power == FB_BLANK_UNBLANK);
1916 		err = asus_wmi_set_devstate(ASUS_WMI_DEVID_BACKLIGHT,
1917 					    ctrl_param, NULL);
1918 		if (asus->driver->quirks->store_backlight_power)
1919 			asus->driver->panel_power = bd->props.power;
1920 
1921 		/* When using scalar brightness, updating the brightness
1922 		 * will mess with the backlight power */
1923 		if (asus->driver->quirks->scalar_panel_brightness)
1924 			return err;
1925 	}
1926 
1927 	if (asus->driver->quirks->scalar_panel_brightness)
1928 		ctrl_param = get_scalar_command(bd);
1929 	else
1930 		ctrl_param = bd->props.brightness;
1931 
1932 	err = asus_wmi_set_devstate(ASUS_WMI_DEVID_BRIGHTNESS,
1933 				    ctrl_param, NULL);
1934 
1935 	return err;
1936 }
1937 
1938 static const struct backlight_ops asus_wmi_bl_ops = {
1939 	.get_brightness = read_brightness,
1940 	.update_status = update_bl_status,
1941 };
1942 
1943 static int asus_wmi_backlight_notify(struct asus_wmi *asus, int code)
1944 {
1945 	struct backlight_device *bd = asus->backlight_device;
1946 	int old = bd->props.brightness;
1947 	int new = old;
1948 
1949 	if (code >= NOTIFY_BRNUP_MIN && code <= NOTIFY_BRNUP_MAX)
1950 		new = code - NOTIFY_BRNUP_MIN + 1;
1951 	else if (code >= NOTIFY_BRNDOWN_MIN && code <= NOTIFY_BRNDOWN_MAX)
1952 		new = code - NOTIFY_BRNDOWN_MIN;
1953 
1954 	bd->props.brightness = new;
1955 	backlight_update_status(bd);
1956 	backlight_force_update(bd, BACKLIGHT_UPDATE_HOTKEY);
1957 
1958 	return old;
1959 }
1960 
1961 static int asus_wmi_backlight_init(struct asus_wmi *asus)
1962 {
1963 	struct backlight_device *bd;
1964 	struct backlight_properties props;
1965 	int max;
1966 	int power;
1967 
1968 	max = read_brightness_max(asus);
1969 	if (max < 0)
1970 		return max;
1971 
1972 	power = read_backlight_power(asus);
1973 	if (power == -ENODEV)
1974 		power = FB_BLANK_UNBLANK;
1975 	else if (power < 0)
1976 		return power;
1977 
1978 	memset(&props, 0, sizeof(struct backlight_properties));
1979 	props.type = BACKLIGHT_PLATFORM;
1980 	props.max_brightness = max;
1981 	bd = backlight_device_register(asus->driver->name,
1982 				       &asus->platform_device->dev, asus,
1983 				       &asus_wmi_bl_ops, &props);
1984 	if (IS_ERR(bd)) {
1985 		pr_err("Could not register backlight device\n");
1986 		return PTR_ERR(bd);
1987 	}
1988 
1989 	asus->backlight_device = bd;
1990 
1991 	if (asus->driver->quirks->store_backlight_power)
1992 		asus->driver->panel_power = power;
1993 
1994 	bd->props.brightness = read_brightness(bd);
1995 	bd->props.power = power;
1996 	backlight_update_status(bd);
1997 
1998 	asus->driver->brightness = bd->props.brightness;
1999 
2000 	return 0;
2001 }
2002 
2003 static void asus_wmi_backlight_exit(struct asus_wmi *asus)
2004 {
2005 	backlight_device_unregister(asus->backlight_device);
2006 
2007 	asus->backlight_device = NULL;
2008 }
2009 
2010 static int is_display_toggle(int code)
2011 {
2012 	/* display toggle keys */
2013 	if ((code >= 0x61 && code <= 0x67) ||
2014 	    (code >= 0x8c && code <= 0x93) ||
2015 	    (code >= 0xa0 && code <= 0xa7) ||
2016 	    (code >= 0xd0 && code <= 0xd5))
2017 		return 1;
2018 
2019 	return 0;
2020 }
2021 
2022 /* Fn-lock ********************************************************************/
2023 
2024 static bool asus_wmi_has_fnlock_key(struct asus_wmi *asus)
2025 {
2026 	u32 result;
2027 
2028 	asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_FNLOCK, &result);
2029 
2030 	return (result & ASUS_WMI_DSTS_PRESENCE_BIT) &&
2031 		!(result & ASUS_WMI_FNLOCK_BIOS_DISABLED);
2032 }
2033 
2034 static void asus_wmi_fnlock_update(struct asus_wmi *asus)
2035 {
2036 	int mode = asus->fnlock_locked;
2037 
2038 	asus_wmi_set_devstate(ASUS_WMI_DEVID_FNLOCK, mode, NULL);
2039 }
2040 
2041 /* WMI events *****************************************************************/
2042 
2043 static int asus_wmi_get_event_code(u32 value)
2044 {
2045 	struct acpi_buffer response = { ACPI_ALLOCATE_BUFFER, NULL };
2046 	union acpi_object *obj;
2047 	acpi_status status;
2048 	int code;
2049 
2050 	status = wmi_get_event_data(value, &response);
2051 	if (ACPI_FAILURE(status)) {
2052 		pr_warn("Failed to get WMI notify code: %s\n",
2053 				acpi_format_exception(status));
2054 		return -EIO;
2055 	}
2056 
2057 	obj = (union acpi_object *)response.pointer;
2058 
2059 	if (obj && obj->type == ACPI_TYPE_INTEGER)
2060 		code = (int)(obj->integer.value & WMI_EVENT_MASK);
2061 	else
2062 		code = -EIO;
2063 
2064 	kfree(obj);
2065 	return code;
2066 }
2067 
2068 static void asus_wmi_handle_event_code(int code, struct asus_wmi *asus)
2069 {
2070 	unsigned int key_value = 1;
2071 	bool autorelease = 1;
2072 	int result, orig_code;
2073 
2074 	orig_code = code;
2075 
2076 	if (asus->driver->key_filter) {
2077 		asus->driver->key_filter(asus->driver, &code, &key_value,
2078 					 &autorelease);
2079 		if (code == ASUS_WMI_KEY_IGNORE)
2080 			return;
2081 	}
2082 
2083 	if (code >= NOTIFY_BRNUP_MIN && code <= NOTIFY_BRNUP_MAX)
2084 		code = ASUS_WMI_BRN_UP;
2085 	else if (code >= NOTIFY_BRNDOWN_MIN && code <= NOTIFY_BRNDOWN_MAX)
2086 		code = ASUS_WMI_BRN_DOWN;
2087 
2088 	if (code == ASUS_WMI_BRN_DOWN || code == ASUS_WMI_BRN_UP) {
2089 		if (acpi_video_get_backlight_type() == acpi_backlight_vendor) {
2090 			asus_wmi_backlight_notify(asus, orig_code);
2091 			return;
2092 		}
2093 	}
2094 
2095 	if (code == NOTIFY_KBD_BRTUP) {
2096 		kbd_led_set_by_kbd(asus, asus->kbd_led_wk + 1);
2097 		return;
2098 	}
2099 	if (code == NOTIFY_KBD_BRTDWN) {
2100 		kbd_led_set_by_kbd(asus, asus->kbd_led_wk - 1);
2101 		return;
2102 	}
2103 	if (code == NOTIFY_KBD_BRTTOGGLE) {
2104 		if (asus->kbd_led_wk == asus->kbd_led.max_brightness)
2105 			kbd_led_set_by_kbd(asus, 0);
2106 		else
2107 			kbd_led_set_by_kbd(asus, asus->kbd_led_wk + 1);
2108 		return;
2109 	}
2110 
2111 	if (code == NOTIFY_FNLOCK_TOGGLE) {
2112 		asus->fnlock_locked = !asus->fnlock_locked;
2113 		asus_wmi_fnlock_update(asus);
2114 		return;
2115 	}
2116 
2117 	if (code == NOTIFY_KBD_DOCK_CHANGE) {
2118 		result = asus_wmi_get_devstate_simple(asus,
2119 						      ASUS_WMI_DEVID_KBD_DOCK);
2120 		if (result >= 0) {
2121 			input_report_switch(asus->inputdev, SW_TABLET_MODE,
2122 					    !result);
2123 			input_sync(asus->inputdev);
2124 		}
2125 		return;
2126 	}
2127 
2128 	if (asus->fan_boost_mode_available && code == NOTIFY_KBD_FBM) {
2129 		fan_boost_mode_switch_next(asus);
2130 		return;
2131 	}
2132 
2133 	if (asus->throttle_thermal_policy_available && code == NOTIFY_KBD_TTP) {
2134 		throttle_thermal_policy_switch_next(asus);
2135 		return;
2136 	}
2137 
2138 	if (is_display_toggle(code) && asus->driver->quirks->no_display_toggle)
2139 		return;
2140 
2141 	if (!sparse_keymap_report_event(asus->inputdev, code,
2142 					key_value, autorelease))
2143 		pr_info("Unknown key %x pressed\n", code);
2144 }
2145 
2146 static void asus_wmi_notify(u32 value, void *context)
2147 {
2148 	struct asus_wmi *asus = context;
2149 	int code;
2150 	int i;
2151 
2152 	for (i = 0; i < WMI_EVENT_QUEUE_SIZE + 1; i++) {
2153 		code = asus_wmi_get_event_code(value);
2154 		if (code < 0) {
2155 			pr_warn("Failed to get notify code: %d\n", code);
2156 			return;
2157 		}
2158 
2159 		if (code == WMI_EVENT_QUEUE_END || code == WMI_EVENT_MASK)
2160 			return;
2161 
2162 		asus_wmi_handle_event_code(code, asus);
2163 
2164 		/*
2165 		 * Double check that queue is present:
2166 		 * ATK (with queue) uses 0xff, ASUSWMI (without) 0xd2.
2167 		 */
2168 		if (!asus->wmi_event_queue || value != WMI_EVENT_VALUE_ATK)
2169 			return;
2170 	}
2171 
2172 	pr_warn("Failed to process event queue, last code: 0x%x\n", code);
2173 }
2174 
2175 static int asus_wmi_notify_queue_flush(struct asus_wmi *asus)
2176 {
2177 	int code;
2178 	int i;
2179 
2180 	for (i = 0; i < WMI_EVENT_QUEUE_SIZE + 1; i++) {
2181 		code = asus_wmi_get_event_code(WMI_EVENT_VALUE_ATK);
2182 		if (code < 0) {
2183 			pr_warn("Failed to get event during flush: %d\n", code);
2184 			return code;
2185 		}
2186 
2187 		if (code == WMI_EVENT_QUEUE_END || code == WMI_EVENT_MASK)
2188 			return 0;
2189 	}
2190 
2191 	pr_warn("Failed to flush event queue\n");
2192 	return -EIO;
2193 }
2194 
2195 /* Sysfs **********************************************************************/
2196 
2197 static ssize_t store_sys_wmi(struct asus_wmi *asus, int devid,
2198 			     const char *buf, size_t count)
2199 {
2200 	u32 retval;
2201 	int err, value;
2202 
2203 	value = asus_wmi_get_devstate_simple(asus, devid);
2204 	if (value < 0)
2205 		return value;
2206 
2207 	err = kstrtoint(buf, 0, &value);
2208 	if (err)
2209 		return err;
2210 
2211 	err = asus_wmi_set_devstate(devid, value, &retval);
2212 	if (err < 0)
2213 		return err;
2214 
2215 	return count;
2216 }
2217 
2218 static ssize_t show_sys_wmi(struct asus_wmi *asus, int devid, char *buf)
2219 {
2220 	int value = asus_wmi_get_devstate_simple(asus, devid);
2221 
2222 	if (value < 0)
2223 		return value;
2224 
2225 	return sprintf(buf, "%d\n", value);
2226 }
2227 
2228 #define ASUS_WMI_CREATE_DEVICE_ATTR(_name, _mode, _cm)			\
2229 	static ssize_t show_##_name(struct device *dev,			\
2230 				    struct device_attribute *attr,	\
2231 				    char *buf)				\
2232 	{								\
2233 		struct asus_wmi *asus = dev_get_drvdata(dev);		\
2234 									\
2235 		return show_sys_wmi(asus, _cm, buf);			\
2236 	}								\
2237 	static ssize_t store_##_name(struct device *dev,		\
2238 				     struct device_attribute *attr,	\
2239 				     const char *buf, size_t count)	\
2240 	{								\
2241 		struct asus_wmi *asus = dev_get_drvdata(dev);		\
2242 									\
2243 		return store_sys_wmi(asus, _cm, buf, count);		\
2244 	}								\
2245 	static struct device_attribute dev_attr_##_name = {		\
2246 		.attr = {						\
2247 			.name = __stringify(_name),			\
2248 			.mode = _mode },				\
2249 		.show   = show_##_name,					\
2250 		.store  = store_##_name,				\
2251 	}
2252 
2253 ASUS_WMI_CREATE_DEVICE_ATTR(touchpad, 0644, ASUS_WMI_DEVID_TOUCHPAD);
2254 ASUS_WMI_CREATE_DEVICE_ATTR(camera, 0644, ASUS_WMI_DEVID_CAMERA);
2255 ASUS_WMI_CREATE_DEVICE_ATTR(cardr, 0644, ASUS_WMI_DEVID_CARDREADER);
2256 ASUS_WMI_CREATE_DEVICE_ATTR(lid_resume, 0644, ASUS_WMI_DEVID_LID_RESUME);
2257 ASUS_WMI_CREATE_DEVICE_ATTR(als_enable, 0644, ASUS_WMI_DEVID_ALS_ENABLE);
2258 
2259 static ssize_t cpufv_store(struct device *dev, struct device_attribute *attr,
2260 			   const char *buf, size_t count)
2261 {
2262 	int value, rv;
2263 
2264 	rv = kstrtoint(buf, 0, &value);
2265 	if (rv)
2266 		return rv;
2267 
2268 	if (value < 0 || value > 2)
2269 		return -EINVAL;
2270 
2271 	rv = asus_wmi_evaluate_method(ASUS_WMI_METHODID_CFVS, value, 0, NULL);
2272 	if (rv < 0)
2273 		return rv;
2274 
2275 	return count;
2276 }
2277 
2278 static DEVICE_ATTR_WO(cpufv);
2279 
2280 static struct attribute *platform_attributes[] = {
2281 	&dev_attr_cpufv.attr,
2282 	&dev_attr_camera.attr,
2283 	&dev_attr_cardr.attr,
2284 	&dev_attr_touchpad.attr,
2285 	&dev_attr_lid_resume.attr,
2286 	&dev_attr_als_enable.attr,
2287 	&dev_attr_fan_boost_mode.attr,
2288 	&dev_attr_throttle_thermal_policy.attr,
2289 	NULL
2290 };
2291 
2292 static umode_t asus_sysfs_is_visible(struct kobject *kobj,
2293 				    struct attribute *attr, int idx)
2294 {
2295 	struct device *dev = container_of(kobj, struct device, kobj);
2296 	struct asus_wmi *asus = dev_get_drvdata(dev);
2297 	bool ok = true;
2298 	int devid = -1;
2299 
2300 	if (attr == &dev_attr_camera.attr)
2301 		devid = ASUS_WMI_DEVID_CAMERA;
2302 	else if (attr == &dev_attr_cardr.attr)
2303 		devid = ASUS_WMI_DEVID_CARDREADER;
2304 	else if (attr == &dev_attr_touchpad.attr)
2305 		devid = ASUS_WMI_DEVID_TOUCHPAD;
2306 	else if (attr == &dev_attr_lid_resume.attr)
2307 		devid = ASUS_WMI_DEVID_LID_RESUME;
2308 	else if (attr == &dev_attr_als_enable.attr)
2309 		devid = ASUS_WMI_DEVID_ALS_ENABLE;
2310 	else if (attr == &dev_attr_fan_boost_mode.attr)
2311 		ok = asus->fan_boost_mode_available;
2312 	else if (attr == &dev_attr_throttle_thermal_policy.attr)
2313 		ok = asus->throttle_thermal_policy_available;
2314 
2315 	if (devid != -1)
2316 		ok = !(asus_wmi_get_devstate_simple(asus, devid) < 0);
2317 
2318 	return ok ? attr->mode : 0;
2319 }
2320 
2321 static const struct attribute_group platform_attribute_group = {
2322 	.is_visible = asus_sysfs_is_visible,
2323 	.attrs = platform_attributes
2324 };
2325 
2326 static void asus_wmi_sysfs_exit(struct platform_device *device)
2327 {
2328 	sysfs_remove_group(&device->dev.kobj, &platform_attribute_group);
2329 }
2330 
2331 static int asus_wmi_sysfs_init(struct platform_device *device)
2332 {
2333 	return sysfs_create_group(&device->dev.kobj, &platform_attribute_group);
2334 }
2335 
2336 /* Platform device ************************************************************/
2337 
2338 static int asus_wmi_platform_init(struct asus_wmi *asus)
2339 {
2340 	struct device *dev = &asus->platform_device->dev;
2341 	char *wmi_uid;
2342 	int rv;
2343 
2344 	/* INIT enable hotkeys on some models */
2345 	if (!asus_wmi_evaluate_method(ASUS_WMI_METHODID_INIT, 0, 0, &rv))
2346 		pr_info("Initialization: %#x\n", rv);
2347 
2348 	/* We don't know yet what to do with this version... */
2349 	if (!asus_wmi_evaluate_method(ASUS_WMI_METHODID_SPEC, 0, 0x9, &rv)) {
2350 		pr_info("BIOS WMI version: %d.%d\n", rv >> 16, rv & 0xFF);
2351 		asus->spec = rv;
2352 	}
2353 
2354 	/*
2355 	 * The SFUN method probably allows the original driver to get the list
2356 	 * of features supported by a given model. For now, 0x0100 or 0x0800
2357 	 * bit signifies that the laptop is equipped with a Wi-Fi MiniPCI card.
2358 	 * The significance of others is yet to be found.
2359 	 */
2360 	if (!asus_wmi_evaluate_method(ASUS_WMI_METHODID_SFUN, 0, 0, &rv)) {
2361 		pr_info("SFUN value: %#x\n", rv);
2362 		asus->sfun = rv;
2363 	}
2364 
2365 	/*
2366 	 * Eee PC and Notebooks seems to have different method_id for DSTS,
2367 	 * but it may also be related to the BIOS's SPEC.
2368 	 * Note, on most Eeepc, there is no way to check if a method exist
2369 	 * or note, while on notebooks, they returns 0xFFFFFFFE on failure,
2370 	 * but once again, SPEC may probably be used for that kind of things.
2371 	 *
2372 	 * Additionally at least TUF Gaming series laptops return nothing for
2373 	 * unknown methods, so the detection in this way is not possible.
2374 	 *
2375 	 * There is strong indication that only ACPI WMI devices that have _UID
2376 	 * equal to "ASUSWMI" use DCTS whereas those with "ATK" use DSTS.
2377 	 */
2378 	wmi_uid = wmi_get_acpi_device_uid(ASUS_WMI_MGMT_GUID);
2379 	if (!wmi_uid)
2380 		return -ENODEV;
2381 
2382 	if (!strcmp(wmi_uid, ASUS_ACPI_UID_ASUSWMI)) {
2383 		dev_info(dev, "Detected ASUSWMI, use DCTS\n");
2384 		asus->dsts_id = ASUS_WMI_METHODID_DCTS;
2385 	} else {
2386 		dev_info(dev, "Detected %s, not ASUSWMI, use DSTS\n", wmi_uid);
2387 		asus->dsts_id = ASUS_WMI_METHODID_DSTS;
2388 	}
2389 
2390 	/*
2391 	 * Some devices can have multiple event codes stored in a queue before
2392 	 * the module load if it was unloaded intermittently after calling
2393 	 * the INIT method (enables event handling). The WMI notify handler is
2394 	 * expected to retrieve all event codes until a retrieved code equals
2395 	 * queue end marker (One or Ones). Old codes are flushed from the queue
2396 	 * upon module load. Not enabling this when it should be has minimal
2397 	 * visible impact so fall back if anything goes wrong.
2398 	 */
2399 	wmi_uid = wmi_get_acpi_device_uid(asus->driver->event_guid);
2400 	if (wmi_uid && !strcmp(wmi_uid, ASUS_ACPI_UID_ATK)) {
2401 		dev_info(dev, "Detected ATK, enable event queue\n");
2402 
2403 		if (!asus_wmi_notify_queue_flush(asus))
2404 			asus->wmi_event_queue = true;
2405 	}
2406 
2407 	/* CWAP allow to define the behavior of the Fn+F2 key,
2408 	 * this method doesn't seems to be present on Eee PCs */
2409 	if (asus->driver->quirks->wapf >= 0)
2410 		asus_wmi_set_devstate(ASUS_WMI_DEVID_CWAP,
2411 				      asus->driver->quirks->wapf, NULL);
2412 
2413 	return 0;
2414 }
2415 
2416 /* debugfs ********************************************************************/
2417 
2418 struct asus_wmi_debugfs_node {
2419 	struct asus_wmi *asus;
2420 	char *name;
2421 	int (*show) (struct seq_file *m, void *data);
2422 };
2423 
2424 static int show_dsts(struct seq_file *m, void *data)
2425 {
2426 	struct asus_wmi *asus = m->private;
2427 	int err;
2428 	u32 retval = -1;
2429 
2430 	err = asus_wmi_get_devstate(asus, asus->debug.dev_id, &retval);
2431 	if (err < 0)
2432 		return err;
2433 
2434 	seq_printf(m, "DSTS(%#x) = %#x\n", asus->debug.dev_id, retval);
2435 
2436 	return 0;
2437 }
2438 
2439 static int show_devs(struct seq_file *m, void *data)
2440 {
2441 	struct asus_wmi *asus = m->private;
2442 	int err;
2443 	u32 retval = -1;
2444 
2445 	err = asus_wmi_set_devstate(asus->debug.dev_id, asus->debug.ctrl_param,
2446 				    &retval);
2447 	if (err < 0)
2448 		return err;
2449 
2450 	seq_printf(m, "DEVS(%#x, %#x) = %#x\n", asus->debug.dev_id,
2451 		   asus->debug.ctrl_param, retval);
2452 
2453 	return 0;
2454 }
2455 
2456 static int show_call(struct seq_file *m, void *data)
2457 {
2458 	struct asus_wmi *asus = m->private;
2459 	struct bios_args args = {
2460 		.arg0 = asus->debug.dev_id,
2461 		.arg1 = asus->debug.ctrl_param,
2462 	};
2463 	struct acpi_buffer input = { (acpi_size) sizeof(args), &args };
2464 	struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER, NULL };
2465 	union acpi_object *obj;
2466 	acpi_status status;
2467 
2468 	status = wmi_evaluate_method(ASUS_WMI_MGMT_GUID,
2469 				     0, asus->debug.method_id,
2470 				     &input, &output);
2471 
2472 	if (ACPI_FAILURE(status))
2473 		return -EIO;
2474 
2475 	obj = (union acpi_object *)output.pointer;
2476 	if (obj && obj->type == ACPI_TYPE_INTEGER)
2477 		seq_printf(m, "%#x(%#x, %#x) = %#x\n", asus->debug.method_id,
2478 			   asus->debug.dev_id, asus->debug.ctrl_param,
2479 			   (u32) obj->integer.value);
2480 	else
2481 		seq_printf(m, "%#x(%#x, %#x) = t:%d\n", asus->debug.method_id,
2482 			   asus->debug.dev_id, asus->debug.ctrl_param,
2483 			   obj ? obj->type : -1);
2484 
2485 	kfree(obj);
2486 
2487 	return 0;
2488 }
2489 
2490 static struct asus_wmi_debugfs_node asus_wmi_debug_files[] = {
2491 	{NULL, "devs", show_devs},
2492 	{NULL, "dsts", show_dsts},
2493 	{NULL, "call", show_call},
2494 };
2495 
2496 static int asus_wmi_debugfs_open(struct inode *inode, struct file *file)
2497 {
2498 	struct asus_wmi_debugfs_node *node = inode->i_private;
2499 
2500 	return single_open(file, node->show, node->asus);
2501 }
2502 
2503 static const struct file_operations asus_wmi_debugfs_io_ops = {
2504 	.owner = THIS_MODULE,
2505 	.open = asus_wmi_debugfs_open,
2506 	.read = seq_read,
2507 	.llseek = seq_lseek,
2508 	.release = single_release,
2509 };
2510 
2511 static void asus_wmi_debugfs_exit(struct asus_wmi *asus)
2512 {
2513 	debugfs_remove_recursive(asus->debug.root);
2514 }
2515 
2516 static void asus_wmi_debugfs_init(struct asus_wmi *asus)
2517 {
2518 	int i;
2519 
2520 	asus->debug.root = debugfs_create_dir(asus->driver->name, NULL);
2521 
2522 	debugfs_create_x32("method_id", S_IRUGO | S_IWUSR, asus->debug.root,
2523 			   &asus->debug.method_id);
2524 
2525 	debugfs_create_x32("dev_id", S_IRUGO | S_IWUSR, asus->debug.root,
2526 			   &asus->debug.dev_id);
2527 
2528 	debugfs_create_x32("ctrl_param", S_IRUGO | S_IWUSR, asus->debug.root,
2529 			   &asus->debug.ctrl_param);
2530 
2531 	for (i = 0; i < ARRAY_SIZE(asus_wmi_debug_files); i++) {
2532 		struct asus_wmi_debugfs_node *node = &asus_wmi_debug_files[i];
2533 
2534 		node->asus = asus;
2535 		debugfs_create_file(node->name, S_IFREG | S_IRUGO,
2536 				    asus->debug.root, node,
2537 				    &asus_wmi_debugfs_io_ops);
2538 	}
2539 }
2540 
2541 /* Init / exit ****************************************************************/
2542 
2543 static int asus_wmi_add(struct platform_device *pdev)
2544 {
2545 	struct platform_driver *pdrv = to_platform_driver(pdev->dev.driver);
2546 	struct asus_wmi_driver *wdrv = to_asus_wmi_driver(pdrv);
2547 	struct asus_wmi *asus;
2548 	const char *chassis_type;
2549 	acpi_status status;
2550 	int err;
2551 	u32 result;
2552 
2553 	asus = kzalloc(sizeof(struct asus_wmi), GFP_KERNEL);
2554 	if (!asus)
2555 		return -ENOMEM;
2556 
2557 	asus->driver = wdrv;
2558 	asus->platform_device = pdev;
2559 	wdrv->platform_device = pdev;
2560 	platform_set_drvdata(asus->platform_device, asus);
2561 
2562 	if (wdrv->detect_quirks)
2563 		wdrv->detect_quirks(asus->driver);
2564 
2565 	err = asus_wmi_platform_init(asus);
2566 	if (err)
2567 		goto fail_platform;
2568 
2569 	err = fan_boost_mode_check_present(asus);
2570 	if (err)
2571 		goto fail_fan_boost_mode;
2572 
2573 	err = throttle_thermal_policy_check_present(asus);
2574 	if (err)
2575 		goto fail_throttle_thermal_policy;
2576 	else
2577 		throttle_thermal_policy_set_default(asus);
2578 
2579 	err = asus_wmi_sysfs_init(asus->platform_device);
2580 	if (err)
2581 		goto fail_sysfs;
2582 
2583 	err = asus_wmi_input_init(asus);
2584 	if (err)
2585 		goto fail_input;
2586 
2587 	err = asus_wmi_fan_init(asus); /* probably no problems on error */
2588 
2589 	err = asus_wmi_hwmon_init(asus);
2590 	if (err)
2591 		goto fail_hwmon;
2592 
2593 	err = asus_wmi_led_init(asus);
2594 	if (err)
2595 		goto fail_leds;
2596 
2597 	asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_WLAN, &result);
2598 	if (result & (ASUS_WMI_DSTS_PRESENCE_BIT | ASUS_WMI_DSTS_USER_BIT))
2599 		asus->driver->wlan_ctrl_by_user = 1;
2600 
2601 	if (!(asus->driver->wlan_ctrl_by_user && ashs_present())) {
2602 		err = asus_wmi_rfkill_init(asus);
2603 		if (err)
2604 			goto fail_rfkill;
2605 	}
2606 
2607 	if (asus->driver->quirks->wmi_force_als_set)
2608 		asus_wmi_set_als();
2609 
2610 	/* Some Asus desktop boards export an acpi-video backlight interface,
2611 	   stop this from showing up */
2612 	chassis_type = dmi_get_system_info(DMI_CHASSIS_TYPE);
2613 	if (chassis_type && !strcmp(chassis_type, "3"))
2614 		acpi_video_set_dmi_backlight_type(acpi_backlight_vendor);
2615 
2616 	if (asus->driver->quirks->wmi_backlight_power)
2617 		acpi_video_set_dmi_backlight_type(acpi_backlight_vendor);
2618 
2619 	if (asus->driver->quirks->wmi_backlight_native)
2620 		acpi_video_set_dmi_backlight_type(acpi_backlight_native);
2621 
2622 	if (asus->driver->quirks->xusb2pr)
2623 		asus_wmi_set_xusb2pr(asus);
2624 
2625 	if (acpi_video_get_backlight_type() == acpi_backlight_vendor) {
2626 		err = asus_wmi_backlight_init(asus);
2627 		if (err && err != -ENODEV)
2628 			goto fail_backlight;
2629 	} else if (asus->driver->quirks->wmi_backlight_set_devstate)
2630 		err = asus_wmi_set_devstate(ASUS_WMI_DEVID_BACKLIGHT, 2, NULL);
2631 
2632 	if (asus_wmi_has_fnlock_key(asus)) {
2633 		asus->fnlock_locked = true;
2634 		asus_wmi_fnlock_update(asus);
2635 	}
2636 
2637 	status = wmi_install_notify_handler(asus->driver->event_guid,
2638 					    asus_wmi_notify, asus);
2639 	if (ACPI_FAILURE(status)) {
2640 		pr_err("Unable to register notify handler - %d\n", status);
2641 		err = -ENODEV;
2642 		goto fail_wmi_handler;
2643 	}
2644 
2645 	asus_wmi_battery_init(asus);
2646 
2647 	asus_wmi_debugfs_init(asus);
2648 
2649 	return 0;
2650 
2651 fail_wmi_handler:
2652 	asus_wmi_backlight_exit(asus);
2653 fail_backlight:
2654 	asus_wmi_rfkill_exit(asus);
2655 fail_rfkill:
2656 	asus_wmi_led_exit(asus);
2657 fail_leds:
2658 fail_hwmon:
2659 	asus_wmi_input_exit(asus);
2660 fail_input:
2661 	asus_wmi_sysfs_exit(asus->platform_device);
2662 fail_sysfs:
2663 fail_throttle_thermal_policy:
2664 fail_fan_boost_mode:
2665 fail_platform:
2666 	kfree(asus);
2667 	return err;
2668 }
2669 
2670 static int asus_wmi_remove(struct platform_device *device)
2671 {
2672 	struct asus_wmi *asus;
2673 
2674 	asus = platform_get_drvdata(device);
2675 	wmi_remove_notify_handler(asus->driver->event_guid);
2676 	asus_wmi_backlight_exit(asus);
2677 	asus_wmi_input_exit(asus);
2678 	asus_wmi_led_exit(asus);
2679 	asus_wmi_rfkill_exit(asus);
2680 	asus_wmi_debugfs_exit(asus);
2681 	asus_wmi_sysfs_exit(asus->platform_device);
2682 	asus_fan_set_auto(asus);
2683 	asus_wmi_battery_exit(asus);
2684 
2685 	kfree(asus);
2686 	return 0;
2687 }
2688 
2689 /* Platform driver - hibernate/resume callbacks *******************************/
2690 
2691 static int asus_hotk_thaw(struct device *device)
2692 {
2693 	struct asus_wmi *asus = dev_get_drvdata(device);
2694 
2695 	if (asus->wlan.rfkill) {
2696 		bool wlan;
2697 
2698 		/*
2699 		 * Work around bios bug - acpi _PTS turns off the wireless led
2700 		 * during suspend.  Normally it restores it on resume, but
2701 		 * we should kick it ourselves in case hibernation is aborted.
2702 		 */
2703 		wlan = asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_WLAN);
2704 		asus_wmi_set_devstate(ASUS_WMI_DEVID_WLAN, wlan, NULL);
2705 	}
2706 
2707 	return 0;
2708 }
2709 
2710 static int asus_hotk_resume(struct device *device)
2711 {
2712 	struct asus_wmi *asus = dev_get_drvdata(device);
2713 
2714 	if (!IS_ERR_OR_NULL(asus->kbd_led.dev))
2715 		kbd_led_update(asus);
2716 
2717 	if (asus_wmi_has_fnlock_key(asus))
2718 		asus_wmi_fnlock_update(asus);
2719 	return 0;
2720 }
2721 
2722 static int asus_hotk_restore(struct device *device)
2723 {
2724 	struct asus_wmi *asus = dev_get_drvdata(device);
2725 	int bl;
2726 
2727 	/* Refresh both wlan rfkill state and pci hotplug */
2728 	if (asus->wlan.rfkill)
2729 		asus_rfkill_hotplug(asus);
2730 
2731 	if (asus->bluetooth.rfkill) {
2732 		bl = !asus_wmi_get_devstate_simple(asus,
2733 						   ASUS_WMI_DEVID_BLUETOOTH);
2734 		rfkill_set_sw_state(asus->bluetooth.rfkill, bl);
2735 	}
2736 	if (asus->wimax.rfkill) {
2737 		bl = !asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_WIMAX);
2738 		rfkill_set_sw_state(asus->wimax.rfkill, bl);
2739 	}
2740 	if (asus->wwan3g.rfkill) {
2741 		bl = !asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_WWAN3G);
2742 		rfkill_set_sw_state(asus->wwan3g.rfkill, bl);
2743 	}
2744 	if (asus->gps.rfkill) {
2745 		bl = !asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_GPS);
2746 		rfkill_set_sw_state(asus->gps.rfkill, bl);
2747 	}
2748 	if (asus->uwb.rfkill) {
2749 		bl = !asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_UWB);
2750 		rfkill_set_sw_state(asus->uwb.rfkill, bl);
2751 	}
2752 	if (!IS_ERR_OR_NULL(asus->kbd_led.dev))
2753 		kbd_led_update(asus);
2754 
2755 	if (asus_wmi_has_fnlock_key(asus))
2756 		asus_wmi_fnlock_update(asus);
2757 	return 0;
2758 }
2759 
2760 static const struct dev_pm_ops asus_pm_ops = {
2761 	.thaw = asus_hotk_thaw,
2762 	.restore = asus_hotk_restore,
2763 	.resume = asus_hotk_resume,
2764 };
2765 
2766 /* Registration ***************************************************************/
2767 
2768 static int asus_wmi_probe(struct platform_device *pdev)
2769 {
2770 	struct platform_driver *pdrv = to_platform_driver(pdev->dev.driver);
2771 	struct asus_wmi_driver *wdrv = to_asus_wmi_driver(pdrv);
2772 	int ret;
2773 
2774 	if (!wmi_has_guid(ASUS_WMI_MGMT_GUID)) {
2775 		pr_warn("ASUS Management GUID not found\n");
2776 		return -ENODEV;
2777 	}
2778 
2779 	if (wdrv->event_guid && !wmi_has_guid(wdrv->event_guid)) {
2780 		pr_warn("ASUS Event GUID not found\n");
2781 		return -ENODEV;
2782 	}
2783 
2784 	if (wdrv->probe) {
2785 		ret = wdrv->probe(pdev);
2786 		if (ret)
2787 			return ret;
2788 	}
2789 
2790 	return asus_wmi_add(pdev);
2791 }
2792 
2793 static bool used;
2794 
2795 int __init_or_module asus_wmi_register_driver(struct asus_wmi_driver *driver)
2796 {
2797 	struct platform_driver *platform_driver;
2798 	struct platform_device *platform_device;
2799 
2800 	if (used)
2801 		return -EBUSY;
2802 
2803 	platform_driver = &driver->platform_driver;
2804 	platform_driver->remove = asus_wmi_remove;
2805 	platform_driver->driver.owner = driver->owner;
2806 	platform_driver->driver.name = driver->name;
2807 	platform_driver->driver.pm = &asus_pm_ops;
2808 
2809 	platform_device = platform_create_bundle(platform_driver,
2810 						 asus_wmi_probe,
2811 						 NULL, 0, NULL, 0);
2812 	if (IS_ERR(platform_device))
2813 		return PTR_ERR(platform_device);
2814 
2815 	used = true;
2816 	return 0;
2817 }
2818 EXPORT_SYMBOL_GPL(asus_wmi_register_driver);
2819 
2820 void asus_wmi_unregister_driver(struct asus_wmi_driver *driver)
2821 {
2822 	platform_device_unregister(driver->platform_device);
2823 	platform_driver_unregister(&driver->platform_driver);
2824 	used = false;
2825 }
2826 EXPORT_SYMBOL_GPL(asus_wmi_unregister_driver);
2827 
2828 static int __init asus_wmi_init(void)
2829 {
2830 	pr_info("ASUS WMI generic driver loaded\n");
2831 	return 0;
2832 }
2833 
2834 static void __exit asus_wmi_exit(void)
2835 {
2836 	pr_info("ASUS WMI generic driver unloaded\n");
2837 }
2838 
2839 module_init(asus_wmi_init);
2840 module_exit(asus_wmi_exit);
2841