]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - drivers/platform/x86/asus-wmi.c
treewide: Replace GPLv2 boilerplate/reference with SPDX - rule 156
[mirror_ubuntu-jammy-kernel.git] / drivers / platform / x86 / asus-wmi.c
CommitLineData
1a59d1b8 1// SPDX-License-Identifier: GPL-2.0-or-later
ee027e4a 2/*
e12e6d94 3 * Asus PC WMI hotkey driver
ee027e4a
YW
4 *
5 * Copyright(C) 2010 Intel Corporation.
57ab7dae 6 * Copyright(C) 2010-2011 Corentin Chary <corentin.chary@gmail.com>
ee027e4a
YW
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>
ee027e4a
YW
12 */
13
81248889
YW
14#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
15
ee027e4a
YW
16#include <linux/kernel.h>
17#include <linux/module.h>
18#include <linux/init.h>
19#include <linux/types.h>
a32f3926 20#include <linux/slab.h>
ee027e4a
YW
21#include <linux/input.h>
22#include <linux/input/sparse-keymap.h>
3d7b1655
YW
23#include <linux/fb.h>
24#include <linux/backlight.h>
084fca63 25#include <linux/leds.h>
ba48fdb9 26#include <linux/rfkill.h>
afa7c886
CC
27#include <linux/pci.h>
28#include <linux/pci_hotplug.h>
e07babde
CC
29#include <linux/hwmon.h>
30#include <linux/hwmon-sysfs.h>
8c1b2d83
CC
31#include <linux/debugfs.h>
32#include <linux/seq_file.h>
ffb6ce70 33#include <linux/platform_data/x86/asus-wmi.h>
45f2c693 34#include <linux/platform_device.h>
6118b8ad 35#include <linux/thermal.h>
8b48463f 36#include <linux/acpi.h>
d8c66f62 37#include <linux/dmi.h>
272c77d5 38#include <acpi/video.h>
ee027e4a 39
e12e6d94 40#include "asus-wmi.h"
45f2c693 41
5909c654 42MODULE_AUTHOR("Corentin Chary <corentin.chary@gmail.com>, "
e12e6d94
CC
43 "Yong Wang <yong.y.wang@intel.com>");
44MODULE_DESCRIPTION("Asus Generic WMI Driver");
ee027e4a
YW
45MODULE_LICENSE("GPL");
46
e12e6d94
CC
47#define to_asus_wmi_driver(pdrv) \
48 (container_of((pdrv), struct asus_wmi_driver, platform_driver))
ee027e4a 49
e12e6d94 50#define ASUS_WMI_MGMT_GUID "97845ED0-4E6D-11DE-8A39-0800200C9A66"
ee027e4a 51
33e0e6fe
CC
52#define NOTIFY_BRNUP_MIN 0x11
53#define NOTIFY_BRNUP_MAX 0x1f
54#define NOTIFY_BRNDOWN_MIN 0x20
55#define NOTIFY_BRNDOWN_MAX 0x2e
487579ba 56#define NOTIFY_FNLOCK_TOGGLE 0x4e
e9809c0b
CC
57#define NOTIFY_KBD_BRTUP 0xc4
58#define NOTIFY_KBD_BRTDWN 0xc5
ed99d29b 59#define NOTIFY_KBD_BRTTOGGLE 0xc7
ee027e4a 60
487579ba
CC
61#define ASUS_WMI_FNLOCK_BIOS_DISABLED BIT(0)
62
53e755c2
KB
63#define ASUS_FAN_DESC "cpu_fan"
64#define ASUS_FAN_MFUN 0x13
65#define ASUS_FAN_SFUN_READ 0x06
66#define ASUS_FAN_SFUN_WRITE 0x07
67#define ASUS_FAN_CTRL_MANUAL 1
68#define ASUS_FAN_CTRL_AUTO 2
69
8023eff1
KCH
70#define USB_INTEL_XUSB2PR 0xD0
71#define PCI_DEVICE_ID_INTEL_LYNXPOINT_LP_XHCI 0x9c31
72
71050ae7
JPRV
73static const char * const ashs_ids[] = { "ATK4001", "ATK4002", NULL };
74
cf48bf9e
JPRV
75static bool ashs_present(void)
76{
77 int i = 0;
78 while (ashs_ids[i]) {
79 if (acpi_dev_found(ashs_ids[i++]))
80 return true;
81 }
82 return false;
83}
84
3d7b1655 85struct bios_args {
d33da3b6
CC
86 u32 arg0;
87 u32 arg1;
88} __packed;
3d7b1655 89
53e755c2
KB
90/*
91 * Struct that's used for all methods called via AGFN. Naming is
92 * identically to the AML code.
93 */
94struct agfn_args {
95 u16 mfun; /* probably "Multi-function" to be called */
96 u16 sfun; /* probably "Sub-function" to be called */
97 u16 len; /* size of the hole struct, including subfunction fields */
98 u8 stas; /* not used by now */
99 u8 err; /* zero on success */
100} __packed;
101
102/* struct used for calling fan read and write methods */
103struct fan_args {
104 struct agfn_args agfn; /* common fields */
105 u8 fan; /* fan number: 0: set auto mode 1: 1st fan */
106 u32 speed; /* read: RPM/100 - write: 0-255 */
107} __packed;
108
8c1b2d83 109/*
e12e6d94 110 * <platform>/ - debugfs root directory
8c1b2d83
CC
111 * dev_id - current dev_id
112 * ctrl_param - current ctrl_param
ef343491 113 * method_id - current method_id
8c1b2d83
CC
114 * devs - call DEVS(dev_id, ctrl_param) and print result
115 * dsts - call DSTS(dev_id) and print result
ef343491 116 * call - call method_id(dev_id, ctrl_param) and print result
8c1b2d83 117 */
e12e6d94 118struct asus_wmi_debug {
8c1b2d83 119 struct dentry *root;
ef343491 120 u32 method_id;
8c1b2d83
CC
121 u32 dev_id;
122 u32 ctrl_param;
123};
124
a7ce3f04
CC
125struct asus_rfkill {
126 struct asus_wmi *asus;
127 struct rfkill *rfkill;
128 u32 dev_id;
129};
130
e12e6d94 131struct asus_wmi {
1d070f89 132 int dsts_id;
46dbca87
CC
133 int spec;
134 int sfun;
1d070f89 135
81248889 136 struct input_dev *inputdev;
3d7b1655 137 struct backlight_device *backlight_device;
27c136c8 138 struct platform_device *platform_device;
084fca63 139
6cae06e6
AK
140 struct led_classdev wlan_led;
141 int wlan_led_wk;
084fca63
CC
142 struct led_classdev tpd_led;
143 int tpd_led_wk;
e9809c0b
CC
144 struct led_classdev kbd_led;
145 int kbd_led_wk;
4c059844
MB
146 struct led_classdev lightbar_led;
147 int lightbar_led_wk;
084fca63
CC
148 struct workqueue_struct *led_workqueue;
149 struct work_struct tpd_led_work;
6cae06e6 150 struct work_struct wlan_led_work;
4c059844 151 struct work_struct lightbar_led_work;
ba48fdb9 152
a7ce3f04
CC
153 struct asus_rfkill wlan;
154 struct asus_rfkill bluetooth;
155 struct asus_rfkill wimax;
156 struct asus_rfkill wwan3g;
43be8bde 157 struct asus_rfkill gps;
a912d329 158 struct asus_rfkill uwb;
8c1b2d83 159
53e755c2
KB
160 bool asus_hwmon_fan_manual_mode;
161 int asus_hwmon_num_fans;
162 int asus_hwmon_pwm;
163
125450f8 164 struct hotplug_slot hotplug_slot;
afa7c886 165 struct mutex hotplug_lock;
279f8f95
CC
166 struct mutex wmi_lock;
167 struct workqueue_struct *hotplug_workqueue;
168 struct work_struct hotplug_work;
afa7c886 169
487579ba
CC
170 bool fnlock_locked;
171
e12e6d94
CC
172 struct asus_wmi_debug debug;
173
174 struct asus_wmi_driver *driver;
81248889
YW
175};
176
e12e6d94 177static int asus_wmi_input_init(struct asus_wmi *asus)
ee027e4a
YW
178{
179 int err;
180
e12e6d94
CC
181 asus->inputdev = input_allocate_device();
182 if (!asus->inputdev)
ee027e4a
YW
183 return -ENOMEM;
184
58a9f397
CC
185 asus->inputdev->name = asus->driver->input_name;
186 asus->inputdev->phys = asus->driver->input_phys;
e12e6d94
CC
187 asus->inputdev->id.bustype = BUS_HOST;
188 asus->inputdev->dev.parent = &asus->platform_device->dev;
39bbde05 189 set_bit(EV_REP, asus->inputdev->evbit);
ee027e4a 190
e12e6d94 191 err = sparse_keymap_setup(asus->inputdev, asus->driver->keymap, NULL);
ee027e4a
YW
192 if (err)
193 goto err_free_dev;
194
e12e6d94 195 err = input_register_device(asus->inputdev);
ee027e4a 196 if (err)
c2ef3a1c 197 goto err_free_dev;
ee027e4a
YW
198
199 return 0;
200
ee027e4a 201err_free_dev:
e12e6d94 202 input_free_device(asus->inputdev);
ee027e4a
YW
203 return err;
204}
205
e12e6d94 206static void asus_wmi_input_exit(struct asus_wmi *asus)
81248889 207{
c2ef3a1c 208 if (asus->inputdev)
e12e6d94 209 input_unregister_device(asus->inputdev);
81248889 210
e12e6d94 211 asus->inputdev = NULL;
81248889
YW
212}
213
ffb6ce70 214int asus_wmi_evaluate_method(u32 method_id, u32 arg0, u32 arg1, u32 *retval)
3d7b1655 215{
d33da3b6
CC
216 struct bios_args args = {
217 .arg0 = arg0,
218 .arg1 = arg1,
219 };
220 struct acpi_buffer input = { (acpi_size) sizeof(args), &args };
3d7b1655 221 struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER, NULL };
3d7b1655 222 acpi_status status;
d33da3b6 223 union acpi_object *obj;
8ad3be1e 224 u32 tmp = 0;
3d7b1655 225
0fe57261 226 status = wmi_evaluate_method(ASUS_WMI_MGMT_GUID, 0, method_id,
afa7c886 227 &input, &output);
3d7b1655
YW
228
229 if (ACPI_FAILURE(status))
d33da3b6 230 goto exit;
3d7b1655
YW
231
232 obj = (union acpi_object *)output.pointer;
233 if (obj && obj->type == ACPI_TYPE_INTEGER)
e12e6d94 234 tmp = (u32) obj->integer.value;
3d7b1655 235
2a3f0064
CC
236 if (retval)
237 *retval = tmp;
3d7b1655
YW
238
239 kfree(obj);
240
d33da3b6
CC
241exit:
242 if (ACPI_FAILURE(status))
243 return -EIO;
244
245 if (tmp == ASUS_WMI_UNSUPPORTED_METHOD)
246 return -ENODEV;
3d7b1655 247
d33da3b6 248 return 0;
3d7b1655 249}
ffb6ce70 250EXPORT_SYMBOL_GPL(asus_wmi_evaluate_method);
3d7b1655 251
53e755c2
KB
252static int asus_wmi_evaluate_method_agfn(const struct acpi_buffer args)
253{
254 struct acpi_buffer input;
255 u64 phys_addr;
256 u32 retval;
257 u32 status = -1;
258
259 /*
260 * Copy to dma capable address otherwise memory corruption occurs as
261 * bios has to be able to access it.
262 */
263 input.pointer = kzalloc(args.length, GFP_DMA | GFP_KERNEL);
264 input.length = args.length;
265 if (!input.pointer)
266 return -ENOMEM;
267 phys_addr = virt_to_phys(input.pointer);
268 memcpy(input.pointer, args.pointer, args.length);
269
270 status = asus_wmi_evaluate_method(ASUS_WMI_METHODID_AGFN,
271 phys_addr, 0, &retval);
272 if (!status)
273 memcpy(args.pointer, input.pointer, args.length);
274
275 kfree(input.pointer);
276 if (status)
277 return -ENXIO;
278
279 return retval;
280}
281
1d070f89 282static int asus_wmi_get_devstate(struct asus_wmi *asus, u32 dev_id, u32 *retval)
3d7b1655 283{
1d070f89 284 return asus_wmi_evaluate_method(asus->dsts_id, dev_id, 0, retval);
d33da3b6 285}
3d7b1655 286
d33da3b6 287static int asus_wmi_set_devstate(u32 dev_id, u32 ctrl_param,
1d070f89 288 u32 *retval)
d33da3b6
CC
289{
290 return asus_wmi_evaluate_method(ASUS_WMI_METHODID_DEVS, dev_id,
291 ctrl_param, retval);
3d7b1655
YW
292}
293
5c95638d 294/* Helper for special devices with magic return codes */
1d070f89
CC
295static int asus_wmi_get_devstate_bits(struct asus_wmi *asus,
296 u32 dev_id, u32 mask)
5c95638d
CC
297{
298 u32 retval = 0;
d33da3b6 299 int err;
5c95638d 300
1d070f89 301 err = asus_wmi_get_devstate(asus, dev_id, &retval);
5c95638d 302
d33da3b6
CC
303 if (err < 0)
304 return err;
5c95638d 305
e12e6d94 306 if (!(retval & ASUS_WMI_DSTS_PRESENCE_BIT))
5c95638d
CC
307 return -ENODEV;
308
a75fe0d7
CC
309 if (mask == ASUS_WMI_DSTS_STATUS_BIT) {
310 if (retval & ASUS_WMI_DSTS_UNKNOWN_BIT)
311 return -ENODEV;
312 }
313
b7187265
CC
314 return retval & mask;
315}
316
1d070f89 317static int asus_wmi_get_devstate_simple(struct asus_wmi *asus, u32 dev_id)
b7187265 318{
1d070f89
CC
319 return asus_wmi_get_devstate_bits(asus, dev_id,
320 ASUS_WMI_DSTS_STATUS_BIT);
5c95638d
CC
321}
322
084fca63
CC
323/*
324 * LEDs
325 */
326/*
327 * These functions actually update the LED's, and are called from a
328 * workqueue. By doing this as separate work rather than when the LED
e12e6d94 329 * subsystem asks, we avoid messing with the Asus ACPI stuff during a
084fca63
CC
330 * potentially bad time, such as a timer interrupt.
331 */
332static void tpd_led_update(struct work_struct *work)
333{
334 int ctrl_param;
e12e6d94 335 struct asus_wmi *asus;
084fca63 336
e12e6d94 337 asus = container_of(work, struct asus_wmi, tpd_led_work);
084fca63 338
e12e6d94
CC
339 ctrl_param = asus->tpd_led_wk;
340 asus_wmi_set_devstate(ASUS_WMI_DEVID_TOUCHPAD_LED, ctrl_param, NULL);
084fca63
CC
341}
342
343static void tpd_led_set(struct led_classdev *led_cdev,
344 enum led_brightness value)
345{
e12e6d94 346 struct asus_wmi *asus;
084fca63 347
e12e6d94 348 asus = container_of(led_cdev, struct asus_wmi, tpd_led);
084fca63 349
e12e6d94
CC
350 asus->tpd_led_wk = !!value;
351 queue_work(asus->led_workqueue, &asus->tpd_led_work);
084fca63
CC
352}
353
e12e6d94 354static int read_tpd_led_state(struct asus_wmi *asus)
084fca63 355{
1d070f89 356 return asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_TOUCHPAD_LED);
084fca63
CC
357}
358
359static enum led_brightness tpd_led_get(struct led_classdev *led_cdev)
360{
e12e6d94 361 struct asus_wmi *asus;
084fca63 362
e12e6d94 363 asus = container_of(led_cdev, struct asus_wmi, tpd_led);
084fca63 364
e12e6d94 365 return read_tpd_led_state(asus);
084fca63
CC
366}
367
9fe44fc9 368static void kbd_led_update(struct asus_wmi *asus)
084fca63 369{
e9809c0b 370 int ctrl_param = 0;
084fca63 371
e9809c0b
CC
372 /*
373 * bits 0-2: level
374 * bit 7: light on/off
375 */
376 if (asus->kbd_led_wk > 0)
377 ctrl_param = 0x80 | (asus->kbd_led_wk & 0x7F);
084fca63 378
e9809c0b
CC
379 asus_wmi_set_devstate(ASUS_WMI_DEVID_KBD_BACKLIGHT, ctrl_param, NULL);
380}
084fca63 381
e9809c0b
CC
382static int kbd_led_read(struct asus_wmi *asus, int *level, int *env)
383{
384 int retval;
385
386 /*
387 * bits 0-2: level
388 * bit 7: light on/off
389 * bit 8-10: environment (0: dark, 1: normal, 2: light)
390 * bit 17: status unknown
391 */
392 retval = asus_wmi_get_devstate_bits(asus, ASUS_WMI_DEVID_KBD_BACKLIGHT,
393 0xFFFF);
394
af965e97 395 /* Unknown status is considered as off */
e9809c0b 396 if (retval == 0x8000)
af965e97 397 retval = 0;
e9809c0b
CC
398
399 if (retval >= 0) {
400 if (level)
c09b2237 401 *level = retval & 0x7F;
e9809c0b
CC
402 if (env)
403 *env = (retval >> 8) & 0x7F;
404 retval = 0;
084fca63
CC
405 }
406
e9809c0b
CC
407 return retval;
408}
409
dbb3d78f 410static void do_kbd_led_set(struct led_classdev *led_cdev, int value)
e9809c0b
CC
411{
412 struct asus_wmi *asus;
dbb3d78f 413 int max_level;
e9809c0b
CC
414
415 asus = container_of(led_cdev, struct asus_wmi, kbd_led);
dbb3d78f 416 max_level = asus->kbd_led.max_brightness;
e9809c0b 417
dbb3d78f
CC
418 if (value > max_level)
419 value = max_level;
e9809c0b
CC
420 else if (value < 0)
421 value = 0;
422
423 asus->kbd_led_wk = value;
9fe44fc9 424 kbd_led_update(asus);
e9809c0b
CC
425}
426
dbb3d78f
CC
427static void kbd_led_set(struct led_classdev *led_cdev,
428 enum led_brightness value)
429{
430 do_kbd_led_set(led_cdev, value);
431}
432
29f6eb53
JHP
433static void kbd_led_set_by_kbd(struct asus_wmi *asus, enum led_brightness value)
434{
435 struct led_classdev *led_cdev = &asus->kbd_led;
436
437 do_kbd_led_set(led_cdev, value);
438 led_classdev_notify_brightness_hw_changed(led_cdev, asus->kbd_led_wk);
439}
440
e9809c0b
CC
441static enum led_brightness kbd_led_get(struct led_classdev *led_cdev)
442{
443 struct asus_wmi *asus;
444 int retval, value;
445
446 asus = container_of(led_cdev, struct asus_wmi, kbd_led);
447
448 retval = kbd_led_read(asus, &value, NULL);
449
450 if (retval < 0)
451 return retval;
452
453 return value;
084fca63
CC
454}
455
6cae06e6
AK
456static int wlan_led_unknown_state(struct asus_wmi *asus)
457{
458 u32 result;
459
460 asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_WIRELESS_LED, &result);
461
462 return result & ASUS_WMI_DSTS_UNKNOWN_BIT;
463}
464
465static int wlan_led_presence(struct asus_wmi *asus)
466{
467 u32 result;
468
469 asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_WIRELESS_LED, &result);
470
471 return result & ASUS_WMI_DSTS_PRESENCE_BIT;
472}
473
474static void wlan_led_update(struct work_struct *work)
475{
476 int ctrl_param;
477 struct asus_wmi *asus;
478
479 asus = container_of(work, struct asus_wmi, wlan_led_work);
480
481 ctrl_param = asus->wlan_led_wk;
482 asus_wmi_set_devstate(ASUS_WMI_DEVID_WIRELESS_LED, ctrl_param, NULL);
483}
484
485static void wlan_led_set(struct led_classdev *led_cdev,
486 enum led_brightness value)
487{
488 struct asus_wmi *asus;
489
490 asus = container_of(led_cdev, struct asus_wmi, wlan_led);
491
492 asus->wlan_led_wk = !!value;
493 queue_work(asus->led_workqueue, &asus->wlan_led_work);
494}
495
496static enum led_brightness wlan_led_get(struct led_classdev *led_cdev)
497{
498 struct asus_wmi *asus;
499 u32 result;
500
501 asus = container_of(led_cdev, struct asus_wmi, wlan_led);
502 asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_WIRELESS_LED, &result);
503
504 return result & ASUS_WMI_DSTS_BRIGHTNESS_MASK;
505}
506
4c059844
MB
507static void lightbar_led_update(struct work_struct *work)
508{
509 struct asus_wmi *asus;
510 int ctrl_param;
511
512 asus = container_of(work, struct asus_wmi, lightbar_led_work);
513
514 ctrl_param = asus->lightbar_led_wk;
515 asus_wmi_set_devstate(ASUS_WMI_DEVID_LIGHTBAR, ctrl_param, NULL);
516}
517
518static void lightbar_led_set(struct led_classdev *led_cdev,
519 enum led_brightness value)
520{
521 struct asus_wmi *asus;
522
523 asus = container_of(led_cdev, struct asus_wmi, lightbar_led);
524
525 asus->lightbar_led_wk = !!value;
526 queue_work(asus->led_workqueue, &asus->lightbar_led_work);
527}
528
529static enum led_brightness lightbar_led_get(struct led_classdev *led_cdev)
530{
531 struct asus_wmi *asus;
532 u32 result;
533
534 asus = container_of(led_cdev, struct asus_wmi, lightbar_led);
535 asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_LIGHTBAR, &result);
536
537 return result & ASUS_WMI_DSTS_LIGHTBAR_MASK;
538}
539
540static int lightbar_led_presence(struct asus_wmi *asus)
541{
542 u32 result;
543
544 asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_LIGHTBAR, &result);
545
546 return result & ASUS_WMI_DSTS_PRESENCE_BIT;
547}
548
e12e6d94 549static void asus_wmi_led_exit(struct asus_wmi *asus)
084fca63 550{
e9298028
AL
551 if (!IS_ERR_OR_NULL(asus->kbd_led.dev))
552 led_classdev_unregister(&asus->kbd_led);
553 if (!IS_ERR_OR_NULL(asus->tpd_led.dev))
e12e6d94 554 led_classdev_unregister(&asus->tpd_led);
6cae06e6
AK
555 if (!IS_ERR_OR_NULL(asus->wlan_led.dev))
556 led_classdev_unregister(&asus->wlan_led);
4c059844
MB
557 if (!IS_ERR_OR_NULL(asus->lightbar_led.dev))
558 led_classdev_unregister(&asus->lightbar_led);
e12e6d94
CC
559 if (asus->led_workqueue)
560 destroy_workqueue(asus->led_workqueue);
084fca63
CC
561}
562
e9809c0b
CC
563static int asus_wmi_led_init(struct asus_wmi *asus)
564{
30734049 565 int rv = 0, led_val;
e9809c0b
CC
566
567 asus->led_workqueue = create_singlethread_workqueue("led_workqueue");
568 if (!asus->led_workqueue)
569 return -ENOMEM;
570
571 if (read_tpd_led_state(asus) >= 0) {
572 INIT_WORK(&asus->tpd_led_work, tpd_led_update);
573
574 asus->tpd_led.name = "asus::touchpad";
575 asus->tpd_led.brightness_set = tpd_led_set;
576 asus->tpd_led.brightness_get = tpd_led_get;
577 asus->tpd_led.max_brightness = 1;
578
579 rv = led_classdev_register(&asus->platform_device->dev,
580 &asus->tpd_led);
581 if (rv)
582 goto error;
583 }
584
30734049
OR
585 led_val = kbd_led_read(asus, NULL, NULL);
586 if (led_val >= 0) {
30734049 587 asus->kbd_led_wk = led_val;
e9809c0b 588 asus->kbd_led.name = "asus::kbd_backlight";
dbb3d78f 589 asus->kbd_led.flags = LED_BRIGHT_HW_CHANGED;
e9809c0b
CC
590 asus->kbd_led.brightness_set = kbd_led_set;
591 asus->kbd_led.brightness_get = kbd_led_get;
592 asus->kbd_led.max_brightness = 3;
593
594 rv = led_classdev_register(&asus->platform_device->dev,
595 &asus->kbd_led);
6cae06e6
AK
596 if (rv)
597 goto error;
598 }
599
f515623c 600 if (wlan_led_presence(asus) && (asus->driver->quirks->wapf > 0)) {
6cae06e6
AK
601 INIT_WORK(&asus->wlan_led_work, wlan_led_update);
602
603 asus->wlan_led.name = "asus::wlan";
604 asus->wlan_led.brightness_set = wlan_led_set;
605 if (!wlan_led_unknown_state(asus))
606 asus->wlan_led.brightness_get = wlan_led_get;
607 asus->wlan_led.flags = LED_CORE_SUSPENDRESUME;
608 asus->wlan_led.max_brightness = 1;
609 asus->wlan_led.default_trigger = "asus-wlan";
610
611 rv = led_classdev_register(&asus->platform_device->dev,
612 &asus->wlan_led);
4c059844
MB
613 if (rv)
614 goto error;
615 }
616
617 if (lightbar_led_presence(asus)) {
618 INIT_WORK(&asus->lightbar_led_work, lightbar_led_update);
619
620 asus->lightbar_led.name = "asus::lightbar";
621 asus->lightbar_led.brightness_set = lightbar_led_set;
622 asus->lightbar_led.brightness_get = lightbar_led_get;
623 asus->lightbar_led.max_brightness = 1;
624
625 rv = led_classdev_register(&asus->platform_device->dev,
626 &asus->lightbar_led);
e9809c0b
CC
627 }
628
629error:
630 if (rv)
631 asus_wmi_led_exit(asus);
632
633 return rv;
634}
635
636
afa7c886
CC
637/*
638 * PCI hotplug (for wlan rfkill)
639 */
e12e6d94 640static bool asus_wlan_rfkill_blocked(struct asus_wmi *asus)
afa7c886 641{
1d070f89 642 int result = asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_WLAN);
afa7c886 643
5c95638d 644 if (result < 0)
afa7c886 645 return false;
5c95638d 646 return !result;
afa7c886
CC
647}
648
e12e6d94 649static void asus_rfkill_hotplug(struct asus_wmi *asus)
afa7c886
CC
650{
651 struct pci_dev *dev;
652 struct pci_bus *bus;
279f8f95 653 bool blocked;
afa7c886
CC
654 bool absent;
655 u32 l;
656
e12e6d94
CC
657 mutex_lock(&asus->wmi_lock);
658 blocked = asus_wlan_rfkill_blocked(asus);
659 mutex_unlock(&asus->wmi_lock);
afa7c886 660
e12e6d94 661 mutex_lock(&asus->hotplug_lock);
8b9ec1da 662 pci_lock_rescan_remove();
afa7c886 663
a7ce3f04
CC
664 if (asus->wlan.rfkill)
665 rfkill_set_sw_state(asus->wlan.rfkill, blocked);
279f8f95 666
125450f8 667 if (asus->hotplug_slot.ops) {
afa7c886
CC
668 bus = pci_find_bus(0, 1);
669 if (!bus) {
5ad77dcf 670 pr_warn("Unable to find PCI bus 1?\n");
afa7c886
CC
671 goto out_unlock;
672 }
673
674 if (pci_bus_read_config_dword(bus, 0, PCI_VENDOR_ID, &l)) {
675 pr_err("Unable to read PCI config space?\n");
676 goto out_unlock;
677 }
678 absent = (l == 0xffffffff);
679
680 if (blocked != absent) {
5ad77dcf
JP
681 pr_warn("BIOS says wireless lan is %s, "
682 "but the pci device is %s\n",
683 blocked ? "blocked" : "unblocked",
684 absent ? "absent" : "present");
685 pr_warn("skipped wireless hotplug as probably "
686 "inappropriate for this model\n");
afa7c886
CC
687 goto out_unlock;
688 }
689
690 if (!blocked) {
691 dev = pci_get_slot(bus, 0);
692 if (dev) {
693 /* Device already present */
694 pci_dev_put(dev);
695 goto out_unlock;
696 }
697 dev = pci_scan_single_device(bus, 0);
698 if (dev) {
699 pci_bus_assign_resources(bus);
c893d133 700 pci_bus_add_device(dev);
afa7c886
CC
701 }
702 } else {
703 dev = pci_get_slot(bus, 0);
704 if (dev) {
210647af 705 pci_stop_and_remove_bus_device(dev);
afa7c886
CC
706 pci_dev_put(dev);
707 }
708 }
709 }
710
711out_unlock:
8b9ec1da 712 pci_unlock_rescan_remove();
e12e6d94 713 mutex_unlock(&asus->hotplug_lock);
afa7c886
CC
714}
715
e12e6d94 716static void asus_rfkill_notify(acpi_handle handle, u32 event, void *data)
afa7c886 717{
e12e6d94 718 struct asus_wmi *asus = data;
afa7c886
CC
719
720 if (event != ACPI_NOTIFY_BUS_CHECK)
721 return;
722
279f8f95 723 /*
e12e6d94 724 * We can't call directly asus_rfkill_hotplug because most
279f8f95
CC
725 * of the time WMBC is still being executed and not reetrant.
726 * There is currently no way to tell ACPICA that we want this
e12e6d94 727 * method to be serialized, we schedule a asus_rfkill_hotplug
279f8f95
CC
728 * call later, in a safer context.
729 */
e12e6d94 730 queue_work(asus->hotplug_workqueue, &asus->hotplug_work);
afa7c886
CC
731}
732
e12e6d94 733static int asus_register_rfkill_notifier(struct asus_wmi *asus, char *node)
afa7c886
CC
734{
735 acpi_status status;
736 acpi_handle handle;
737
738 status = acpi_get_handle(NULL, node, &handle);
739
740 if (ACPI_SUCCESS(status)) {
741 status = acpi_install_notify_handler(handle,
742 ACPI_SYSTEM_NOTIFY,
e12e6d94 743 asus_rfkill_notify, asus);
afa7c886 744 if (ACPI_FAILURE(status))
5ad77dcf 745 pr_warn("Failed to register notify on %s\n", node);
afa7c886
CC
746 } else
747 return -ENODEV;
748
749 return 0;
750}
751
e12e6d94 752static void asus_unregister_rfkill_notifier(struct asus_wmi *asus, char *node)
afa7c886
CC
753{
754 acpi_status status = AE_OK;
755 acpi_handle handle;
756
757 status = acpi_get_handle(NULL, node, &handle);
758
759 if (ACPI_SUCCESS(status)) {
760 status = acpi_remove_notify_handler(handle,
e12e6d94
CC
761 ACPI_SYSTEM_NOTIFY,
762 asus_rfkill_notify);
afa7c886
CC
763 if (ACPI_FAILURE(status))
764 pr_err("Error removing rfkill notify handler %s\n",
e12e6d94 765 node);
afa7c886
CC
766 }
767}
768
e12e6d94
CC
769static int asus_get_adapter_status(struct hotplug_slot *hotplug_slot,
770 u8 *value)
afa7c886 771{
125450f8
LW
772 struct asus_wmi *asus = container_of(hotplug_slot,
773 struct asus_wmi, hotplug_slot);
1d070f89 774 int result = asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_WLAN);
afa7c886 775
5c95638d
CC
776 if (result < 0)
777 return result;
afa7c886 778
5c95638d 779 *value = !!result;
afa7c886
CC
780 return 0;
781}
782
81c4b5bf 783static const struct hotplug_slot_ops asus_hotplug_slot_ops = {
e12e6d94
CC
784 .get_adapter_status = asus_get_adapter_status,
785 .get_power_status = asus_get_adapter_status,
afa7c886
CC
786};
787
e12e6d94 788static void asus_hotplug_work(struct work_struct *work)
279f8f95 789{
e12e6d94 790 struct asus_wmi *asus;
279f8f95 791
e12e6d94
CC
792 asus = container_of(work, struct asus_wmi, hotplug_work);
793 asus_rfkill_hotplug(asus);
279f8f95
CC
794}
795
e12e6d94 796static int asus_setup_pci_hotplug(struct asus_wmi *asus)
afa7c886
CC
797{
798 int ret = -ENOMEM;
799 struct pci_bus *bus = pci_find_bus(0, 1);
800
801 if (!bus) {
802 pr_err("Unable to find wifi PCI bus\n");
803 return -ENODEV;
804 }
805
e12e6d94
CC
806 asus->hotplug_workqueue =
807 create_singlethread_workqueue("hotplug_workqueue");
808 if (!asus->hotplug_workqueue)
279f8f95
CC
809 goto error_workqueue;
810
e12e6d94 811 INIT_WORK(&asus->hotplug_work, asus_hotplug_work);
279f8f95 812
125450f8 813 asus->hotplug_slot.ops = &asus_hotplug_slot_ops;
afa7c886 814
125450f8 815 ret = pci_hp_register(&asus->hotplug_slot, bus, 0, "asus-wifi");
afa7c886
CC
816 if (ret) {
817 pr_err("Unable to register hotplug slot - %d\n", ret);
818 goto error_register;
819 }
820
821 return 0;
822
823error_register:
125450f8 824 asus->hotplug_slot.ops = NULL;
e12e6d94 825 destroy_workqueue(asus->hotplug_workqueue);
279f8f95 826error_workqueue:
afa7c886
CC
827 return ret;
828}
829
ba48fdb9
CC
830/*
831 * Rfkill devices
832 */
e12e6d94 833static int asus_rfkill_set(void *data, bool blocked)
ba48fdb9 834{
a7ce3f04 835 struct asus_rfkill *priv = data;
ba48fdb9 836 u32 ctrl_param = !blocked;
a50bd128 837 u32 dev_id = priv->dev_id;
ba48fdb9 838
a50bd128
AK
839 /*
840 * If the user bit is set, BIOS can't set and record the wlan status,
841 * it will report the value read from id ASUS_WMI_DEVID_WLAN_LED
842 * while we query the wlan status through WMI(ASUS_WMI_DEVID_WLAN).
843 * So, we have to record wlan status in id ASUS_WMI_DEVID_WLAN_LED
844 * while setting the wlan status through WMI.
845 * This is also the behavior that windows app will do.
846 */
847 if ((dev_id == ASUS_WMI_DEVID_WLAN) &&
848 priv->asus->driver->wlan_ctrl_by_user)
849 dev_id = ASUS_WMI_DEVID_WLAN_LED;
850
851 return asus_wmi_set_devstate(dev_id, ctrl_param, NULL);
ba48fdb9
CC
852}
853
e12e6d94 854static void asus_rfkill_query(struct rfkill *rfkill, void *data)
ba48fdb9 855{
a7ce3f04 856 struct asus_rfkill *priv = data;
5c95638d 857 int result;
ba48fdb9 858
1d070f89 859 result = asus_wmi_get_devstate_simple(priv->asus, priv->dev_id);
ba48fdb9 860
5c95638d 861 if (result < 0)
e12e6d94 862 return;
ba48fdb9 863
a7ce3f04 864 rfkill_set_sw_state(priv->rfkill, !result);
ba48fdb9
CC
865}
866
e12e6d94 867static int asus_rfkill_wlan_set(void *data, bool blocked)
279f8f95 868{
a7ce3f04
CC
869 struct asus_rfkill *priv = data;
870 struct asus_wmi *asus = priv->asus;
279f8f95
CC
871 int ret;
872
873 /*
874 * This handler is enabled only if hotplug is enabled.
e12e6d94 875 * In this case, the asus_wmi_set_devstate() will
279f8f95
CC
876 * trigger a wmi notification and we need to wait
877 * this call to finish before being able to call
878 * any wmi method
879 */
e12e6d94 880 mutex_lock(&asus->wmi_lock);
a7ce3f04 881 ret = asus_rfkill_set(data, blocked);
e12e6d94 882 mutex_unlock(&asus->wmi_lock);
279f8f95
CC
883 return ret;
884}
885
e12e6d94
CC
886static const struct rfkill_ops asus_rfkill_wlan_ops = {
887 .set_block = asus_rfkill_wlan_set,
a7ce3f04 888 .query = asus_rfkill_query,
279f8f95
CC
889};
890
e12e6d94
CC
891static const struct rfkill_ops asus_rfkill_ops = {
892 .set_block = asus_rfkill_set,
893 .query = asus_rfkill_query,
ba48fdb9
CC
894};
895
e12e6d94 896static int asus_new_rfkill(struct asus_wmi *asus,
a7ce3f04 897 struct asus_rfkill *arfkill,
e12e6d94 898 const char *name, enum rfkill_type type, int dev_id)
ba48fdb9 899{
1d070f89 900 int result = asus_wmi_get_devstate_simple(asus, dev_id);
a7ce3f04 901 struct rfkill **rfkill = &arfkill->rfkill;
ba48fdb9 902
5c95638d
CC
903 if (result < 0)
904 return result;
ba48fdb9 905
a7ce3f04
CC
906 arfkill->dev_id = dev_id;
907 arfkill->asus = asus;
908
c87992d1
AK
909 if (dev_id == ASUS_WMI_DEVID_WLAN &&
910 asus->driver->quirks->hotplug_wireless)
e12e6d94 911 *rfkill = rfkill_alloc(name, &asus->platform_device->dev, type,
a7ce3f04 912 &asus_rfkill_wlan_ops, arfkill);
279f8f95 913 else
e12e6d94 914 *rfkill = rfkill_alloc(name, &asus->platform_device->dev, type,
a7ce3f04 915 &asus_rfkill_ops, arfkill);
ba48fdb9
CC
916
917 if (!*rfkill)
918 return -EINVAL;
919
e8f56c80 920 if ((dev_id == ASUS_WMI_DEVID_WLAN) &&
f515623c 921 (asus->driver->quirks->wapf > 0))
6cae06e6
AK
922 rfkill_set_led_trigger_name(*rfkill, "asus-wlan");
923
5c95638d 924 rfkill_init_sw_state(*rfkill, !result);
ba48fdb9
CC
925 result = rfkill_register(*rfkill);
926 if (result) {
927 rfkill_destroy(*rfkill);
928 *rfkill = NULL;
929 return result;
930 }
931 return 0;
932}
933
e12e6d94 934static void asus_wmi_rfkill_exit(struct asus_wmi *asus)
ba48fdb9 935{
cf48bf9e
JPRV
936 if (asus->driver->wlan_ctrl_by_user && ashs_present())
937 return;
938
e12e6d94
CC
939 asus_unregister_rfkill_notifier(asus, "\\_SB.PCI0.P0P5");
940 asus_unregister_rfkill_notifier(asus, "\\_SB.PCI0.P0P6");
941 asus_unregister_rfkill_notifier(asus, "\\_SB.PCI0.P0P7");
a7ce3f04
CC
942 if (asus->wlan.rfkill) {
943 rfkill_unregister(asus->wlan.rfkill);
944 rfkill_destroy(asus->wlan.rfkill);
945 asus->wlan.rfkill = NULL;
ba48fdb9 946 }
afa7c886
CC
947 /*
948 * Refresh pci hotplug in case the rfkill state was changed after
e12e6d94 949 * asus_unregister_rfkill_notifier()
afa7c886 950 */
e12e6d94 951 asus_rfkill_hotplug(asus);
125450f8
LW
952 if (asus->hotplug_slot.ops)
953 pci_hp_deregister(&asus->hotplug_slot);
e12e6d94
CC
954 if (asus->hotplug_workqueue)
955 destroy_workqueue(asus->hotplug_workqueue);
956
a7ce3f04
CC
957 if (asus->bluetooth.rfkill) {
958 rfkill_unregister(asus->bluetooth.rfkill);
959 rfkill_destroy(asus->bluetooth.rfkill);
960 asus->bluetooth.rfkill = NULL;
ba48fdb9 961 }
a7ce3f04
CC
962 if (asus->wimax.rfkill) {
963 rfkill_unregister(asus->wimax.rfkill);
964 rfkill_destroy(asus->wimax.rfkill);
965 asus->wimax.rfkill = NULL;
2e9e159d 966 }
a7ce3f04
CC
967 if (asus->wwan3g.rfkill) {
968 rfkill_unregister(asus->wwan3g.rfkill);
969 rfkill_destroy(asus->wwan3g.rfkill);
970 asus->wwan3g.rfkill = NULL;
ba48fdb9 971 }
43be8bde
CC
972 if (asus->gps.rfkill) {
973 rfkill_unregister(asus->gps.rfkill);
974 rfkill_destroy(asus->gps.rfkill);
975 asus->gps.rfkill = NULL;
976 }
a912d329
CC
977 if (asus->uwb.rfkill) {
978 rfkill_unregister(asus->uwb.rfkill);
979 rfkill_destroy(asus->uwb.rfkill);
980 asus->uwb.rfkill = NULL;
981 }
ba48fdb9
CC
982}
983
e12e6d94 984static int asus_wmi_rfkill_init(struct asus_wmi *asus)
ba48fdb9
CC
985{
986 int result = 0;
987
e12e6d94
CC
988 mutex_init(&asus->hotplug_lock);
989 mutex_init(&asus->wmi_lock);
afa7c886 990
a7ce3f04
CC
991 result = asus_new_rfkill(asus, &asus->wlan, "asus-wlan",
992 RFKILL_TYPE_WLAN, ASUS_WMI_DEVID_WLAN);
ba48fdb9
CC
993
994 if (result && result != -ENODEV)
995 goto exit;
996
a7ce3f04 997 result = asus_new_rfkill(asus, &asus->bluetooth,
e12e6d94
CC
998 "asus-bluetooth", RFKILL_TYPE_BLUETOOTH,
999 ASUS_WMI_DEVID_BLUETOOTH);
ba48fdb9
CC
1000
1001 if (result && result != -ENODEV)
1002 goto exit;
1003
a7ce3f04
CC
1004 result = asus_new_rfkill(asus, &asus->wimax, "asus-wimax",
1005 RFKILL_TYPE_WIMAX, ASUS_WMI_DEVID_WIMAX);
2e9e159d
CC
1006
1007 if (result && result != -ENODEV)
1008 goto exit;
1009
a7ce3f04
CC
1010 result = asus_new_rfkill(asus, &asus->wwan3g, "asus-wwan3g",
1011 RFKILL_TYPE_WWAN, ASUS_WMI_DEVID_WWAN3G);
ba48fdb9
CC
1012
1013 if (result && result != -ENODEV)
1014 goto exit;
1015
43be8bde
CC
1016 result = asus_new_rfkill(asus, &asus->gps, "asus-gps",
1017 RFKILL_TYPE_GPS, ASUS_WMI_DEVID_GPS);
1018
1019 if (result && result != -ENODEV)
1020 goto exit;
1021
a912d329
CC
1022 result = asus_new_rfkill(asus, &asus->uwb, "asus-uwb",
1023 RFKILL_TYPE_UWB, ASUS_WMI_DEVID_UWB);
1024
1025 if (result && result != -ENODEV)
1026 goto exit;
1027
c87992d1 1028 if (!asus->driver->quirks->hotplug_wireless)
c14d4b8e
CC
1029 goto exit;
1030
e12e6d94 1031 result = asus_setup_pci_hotplug(asus);
afa7c886
CC
1032 /*
1033 * If we get -EBUSY then something else is handling the PCI hotplug -
1034 * don't fail in this case
1035 */
1036 if (result == -EBUSY)
1037 result = 0;
1038
e12e6d94
CC
1039 asus_register_rfkill_notifier(asus, "\\_SB.PCI0.P0P5");
1040 asus_register_rfkill_notifier(asus, "\\_SB.PCI0.P0P6");
1041 asus_register_rfkill_notifier(asus, "\\_SB.PCI0.P0P7");
afa7c886
CC
1042 /*
1043 * Refresh pci hotplug in case the rfkill state was changed during
1044 * setup.
1045 */
e12e6d94 1046 asus_rfkill_hotplug(asus);
afa7c886 1047
ba48fdb9
CC
1048exit:
1049 if (result && result != -ENODEV)
e12e6d94 1050 asus_wmi_rfkill_exit(asus);
ba48fdb9
CC
1051
1052 if (result == -ENODEV)
1053 result = 0;
1054
1055 return result;
1056}
1057
8023eff1
KCH
1058static void asus_wmi_set_xusb2pr(struct asus_wmi *asus)
1059{
1060 struct pci_dev *xhci_pdev;
1061 u32 orig_ports_available;
1062 u32 ports_available = asus->driver->quirks->xusb2pr;
1063
1064 xhci_pdev = pci_get_device(PCI_VENDOR_ID_INTEL,
1065 PCI_DEVICE_ID_INTEL_LYNXPOINT_LP_XHCI,
1066 NULL);
1067
1068 if (!xhci_pdev)
1069 return;
1070
1071 pci_read_config_dword(xhci_pdev, USB_INTEL_XUSB2PR,
1072 &orig_ports_available);
1073
1074 pci_write_config_dword(xhci_pdev, USB_INTEL_XUSB2PR,
1075 cpu_to_le32(ports_available));
1076
1077 pr_info("set USB_INTEL_XUSB2PR old: 0x%04x, new: 0x%04x\n",
1078 orig_ports_available, ports_available);
1079}
1080
e9b61518
OR
1081/*
1082 * Some devices dont support or have borcken get_als method
1083 * but still support set method.
1084 */
1085static void asus_wmi_set_als(void)
1086{
1087 asus_wmi_set_devstate(ASUS_WMI_DEVID_ALS_ENABLE, 1, NULL);
1088}
1089
e07babde
CC
1090/*
1091 * Hwmon device
1092 */
53e755c2
KB
1093static int asus_hwmon_agfn_fan_speed_read(struct asus_wmi *asus, int fan,
1094 int *speed)
1095{
1096 struct fan_args args = {
1097 .agfn.len = sizeof(args),
1098 .agfn.mfun = ASUS_FAN_MFUN,
1099 .agfn.sfun = ASUS_FAN_SFUN_READ,
1100 .fan = fan,
1101 .speed = 0,
1102 };
1103 struct acpi_buffer input = { (acpi_size) sizeof(args), &args };
1104 int status;
1105
1106 if (fan != 1)
1107 return -EINVAL;
1108
1109 status = asus_wmi_evaluate_method_agfn(input);
1110
1111 if (status || args.agfn.err)
1112 return -ENXIO;
1113
1114 if (speed)
1115 *speed = args.speed;
1116
1117 return 0;
1118}
1119
1120static int asus_hwmon_agfn_fan_speed_write(struct asus_wmi *asus, int fan,
1121 int *speed)
1122{
1123 struct fan_args args = {
1124 .agfn.len = sizeof(args),
1125 .agfn.mfun = ASUS_FAN_MFUN,
1126 .agfn.sfun = ASUS_FAN_SFUN_WRITE,
1127 .fan = fan,
1128 .speed = speed ? *speed : 0,
1129 };
1130 struct acpi_buffer input = { (acpi_size) sizeof(args), &args };
1131 int status;
1132
1133 /* 1: for setting 1st fan's speed 0: setting auto mode */
1134 if (fan != 1 && fan != 0)
1135 return -EINVAL;
1136
1137 status = asus_wmi_evaluate_method_agfn(input);
1138
1139 if (status || args.agfn.err)
1140 return -ENXIO;
1141
1142 if (speed && fan == 1)
1143 asus->asus_hwmon_pwm = *speed;
1144
1145 return 0;
1146}
1147
1148/*
1149 * Check if we can read the speed of one fan. If true we assume we can also
1150 * control it.
1151 */
1152static int asus_hwmon_get_fan_number(struct asus_wmi *asus, int *num_fans)
1153{
1154 int status;
1155 int speed = 0;
1156
1157 *num_fans = 0;
1158
1159 status = asus_hwmon_agfn_fan_speed_read(asus, 1, &speed);
1160 if (!status)
1161 *num_fans = 1;
1162
1163 return 0;
1164}
1165
1166static int asus_hwmon_fan_set_auto(struct asus_wmi *asus)
1167{
1168 int status;
1169
1170 status = asus_hwmon_agfn_fan_speed_write(asus, 0, NULL);
1171 if (status)
1172 return -ENXIO;
1173
1174 asus->asus_hwmon_fan_manual_mode = false;
1175
1176 return 0;
1177}
1178
1179static int asus_hwmon_fan_rpm_show(struct device *dev, int fan)
e07babde
CC
1180{
1181 struct asus_wmi *asus = dev_get_drvdata(dev);
53e755c2
KB
1182 int value;
1183 int ret;
1184
1185 /* no speed readable on manual mode */
1186 if (asus->asus_hwmon_fan_manual_mode)
1187 return -ENXIO;
1188
1189 ret = asus_hwmon_agfn_fan_speed_read(asus, fan+1, &value);
1190 if (ret) {
1191 pr_warn("reading fan speed failed: %d\n", ret);
1192 return -ENXIO;
1193 }
1194
1195 return value;
1196}
1197
1198static void asus_hwmon_pwm_show(struct asus_wmi *asus, int fan, int *value)
1199{
e07babde
CC
1200 int err;
1201
53e755c2
KB
1202 if (asus->asus_hwmon_pwm >= 0) {
1203 *value = asus->asus_hwmon_pwm;
1204 return;
1205 }
e07babde 1206
53e755c2 1207 err = asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_FAN_CTRL, value);
e07babde 1208 if (err < 0)
53e755c2 1209 return;
e07babde 1210
53e755c2
KB
1211 *value &= 0xFF;
1212
1213 if (*value == 1) /* Low Speed */
1214 *value = 85;
1215 else if (*value == 2)
1216 *value = 170;
1217 else if (*value == 3)
1218 *value = 255;
1219 else if (*value) {
1220 pr_err("Unknown fan speed %#x\n", *value);
1221 *value = -1;
e07babde 1222 }
53e755c2
KB
1223}
1224
1225static ssize_t pwm1_show(struct device *dev,
1226 struct device_attribute *attr,
1227 char *buf)
1228{
1229 struct asus_wmi *asus = dev_get_drvdata(dev);
1230 int value;
1231
1232 asus_hwmon_pwm_show(asus, 0, &value);
e07babde
CC
1233
1234 return sprintf(buf, "%d\n", value);
1235}
1236
53e755c2
KB
1237static ssize_t pwm1_store(struct device *dev,
1238 struct device_attribute *attr,
1239 const char *buf, size_t count) {
1240 struct asus_wmi *asus = dev_get_drvdata(dev);
1241 int value;
1242 int state;
1243 int ret;
1244
1245 ret = kstrtouint(buf, 10, &value);
1246
1247 if (ret)
1248 return ret;
1249
1250 value = clamp(value, 0, 255);
1251
1252 state = asus_hwmon_agfn_fan_speed_write(asus, 1, &value);
1253 if (state)
1254 pr_warn("Setting fan speed failed: %d\n", state);
1255 else
1256 asus->asus_hwmon_fan_manual_mode = true;
1257
1258 return count;
1259}
1260
1261static ssize_t fan1_input_show(struct device *dev,
1262 struct device_attribute *attr,
1263 char *buf)
1264{
1265 int value = asus_hwmon_fan_rpm_show(dev, 0);
1266
1267 return sprintf(buf, "%d\n", value < 0 ? -1 : value*100);
1268
1269}
1270
1271static ssize_t pwm1_enable_show(struct device *dev,
1272 struct device_attribute *attr,
1273 char *buf)
1274{
1275 struct asus_wmi *asus = dev_get_drvdata(dev);
1276
1277 if (asus->asus_hwmon_fan_manual_mode)
1278 return sprintf(buf, "%d\n", ASUS_FAN_CTRL_MANUAL);
1279
1280 return sprintf(buf, "%d\n", ASUS_FAN_CTRL_AUTO);
1281}
1282
1283static ssize_t pwm1_enable_store(struct device *dev,
1284 struct device_attribute *attr,
1285 const char *buf, size_t count)
1286{
1287 struct asus_wmi *asus = dev_get_drvdata(dev);
1288 int status = 0;
1289 int state;
1290 int ret;
1291
1292 ret = kstrtouint(buf, 10, &state);
1293
1294 if (ret)
1295 return ret;
1296
1297 if (state == ASUS_FAN_CTRL_MANUAL)
1298 asus->asus_hwmon_fan_manual_mode = true;
1299 else
1300 status = asus_hwmon_fan_set_auto(asus);
1301
1302 if (status)
1303 return status;
1304
1305 return count;
1306}
1307
1308static ssize_t fan1_label_show(struct device *dev,
1309 struct device_attribute *attr,
1310 char *buf)
1311{
1312 return sprintf(buf, "%s\n", ASUS_FAN_DESC);
1313}
1314
6118b8ad
CC
1315static ssize_t asus_hwmon_temp1(struct device *dev,
1316 struct device_attribute *attr,
1317 char *buf)
1318{
1319 struct asus_wmi *asus = dev_get_drvdata(dev);
1320 u32 value;
1321 int err;
1322
1323 err = asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_THERMAL_CTRL, &value);
1324
1325 if (err < 0)
1326 return err;
1327
e866a2e3 1328 value = DECI_KELVIN_TO_CELSIUS((value & 0xFFFF)) * 1000;
6118b8ad
CC
1329
1330 return sprintf(buf, "%d\n", value);
1331}
1332
53e755c2
KB
1333/* Fan1 */
1334static DEVICE_ATTR_RW(pwm1);
1335static DEVICE_ATTR_RW(pwm1_enable);
1336static DEVICE_ATTR_RO(fan1_input);
1337static DEVICE_ATTR_RO(fan1_label);
1338
1339/* Temperature */
50a639fb 1340static DEVICE_ATTR(temp1_input, S_IRUGO, asus_hwmon_temp1, NULL);
e07babde
CC
1341
1342static struct attribute *hwmon_attributes[] = {
50a639fb 1343 &dev_attr_pwm1.attr,
53e755c2
KB
1344 &dev_attr_pwm1_enable.attr,
1345 &dev_attr_fan1_input.attr,
1346 &dev_attr_fan1_label.attr,
1347
50a639fb 1348 &dev_attr_temp1_input.attr,
e07babde
CC
1349 NULL
1350};
1351
587a1f16 1352static umode_t asus_hwmon_sysfs_is_visible(struct kobject *kobj,
e02431d6 1353 struct attribute *attr, int idx)
e07babde
CC
1354{
1355 struct device *dev = container_of(kobj, struct device, kobj);
1356 struct platform_device *pdev = to_platform_device(dev->parent);
1357 struct asus_wmi *asus = platform_get_drvdata(pdev);
e07babde 1358 int dev_id = -1;
53e755c2 1359 int fan_attr = -1;
e07babde 1360 u32 value = ASUS_WMI_UNSUPPORTED_METHOD;
53e755c2 1361 bool ok = true;
e07babde 1362
50a639fb 1363 if (attr == &dev_attr_pwm1.attr)
e07babde 1364 dev_id = ASUS_WMI_DEVID_FAN_CTRL;
50a639fb 1365 else if (attr == &dev_attr_temp1_input.attr)
e02431d6 1366 dev_id = ASUS_WMI_DEVID_THERMAL_CTRL;
e07babde 1367
53e755c2
KB
1368
1369 if (attr == &dev_attr_fan1_input.attr
1370 || attr == &dev_attr_fan1_label.attr
1371 || attr == &dev_attr_pwm1.attr
1372 || attr == &dev_attr_pwm1_enable.attr) {
1373 fan_attr = 1;
1374 }
1375
e07babde
CC
1376 if (dev_id != -1) {
1377 int err = asus_wmi_get_devstate(asus, dev_id, &value);
1378
53e755c2 1379 if (err < 0 && fan_attr == -1)
e772aed3 1380 return 0; /* can't return negative here */
e07babde
CC
1381 }
1382
1383 if (dev_id == ASUS_WMI_DEVID_FAN_CTRL) {
1384 /*
1385 * We need to find a better way, probably using sfun,
1386 * bits or spec ...
1387 * Currently we disable it if:
1388 * - ASUS_WMI_UNSUPPORTED_METHOD is returned
1389 * - reverved bits are non-zero
1390 * - sfun and presence bit are not set
1391 */
49979d09 1392 if (value == ASUS_WMI_UNSUPPORTED_METHOD || value & 0xFFF80000
e07babde
CC
1393 || (!asus->sfun && !(value & ASUS_WMI_DSTS_PRESENCE_BIT)))
1394 ok = false;
53e755c2
KB
1395 else
1396 ok = fan_attr <= asus->asus_hwmon_num_fans;
e02431d6
CC
1397 } else if (dev_id == ASUS_WMI_DEVID_THERMAL_CTRL) {
1398 /* If value is zero, something is clearly wrong */
53e755c2 1399 if (!value)
e02431d6 1400 ok = false;
53e755c2
KB
1401 } else if (fan_attr <= asus->asus_hwmon_num_fans && fan_attr != -1) {
1402 ok = true;
1403 } else {
1404 ok = false;
e07babde
CC
1405 }
1406
1407 return ok ? attr->mode : 0;
1408}
1409
e90d9ba8 1410static const struct attribute_group hwmon_attribute_group = {
e07babde
CC
1411 .is_visible = asus_hwmon_sysfs_is_visible,
1412 .attrs = hwmon_attributes
1413};
50a639fb 1414__ATTRIBUTE_GROUPS(hwmon_attribute);
e07babde
CC
1415
1416static int asus_wmi_hwmon_init(struct asus_wmi *asus)
1417{
1418 struct device *hwmon;
e07babde 1419
50a639fb
GR
1420 hwmon = hwmon_device_register_with_groups(&asus->platform_device->dev,
1421 "asus", asus,
1422 hwmon_attribute_groups);
e07babde
CC
1423 if (IS_ERR(hwmon)) {
1424 pr_err("Could not register asus hwmon device\n");
1425 return PTR_ERR(hwmon);
1426 }
50a639fb 1427 return 0;
e07babde
CC
1428}
1429
084fca63
CC
1430/*
1431 * Backlight
1432 */
1d070f89 1433static int read_backlight_power(struct asus_wmi *asus)
b7187265 1434{
6e0044be
AK
1435 int ret;
1436 if (asus->driver->quirks->store_backlight_power)
1437 ret = !asus->driver->panel_power;
1438 else
1439 ret = asus_wmi_get_devstate_simple(asus,
1440 ASUS_WMI_DEVID_BACKLIGHT);
b7187265
CC
1441
1442 if (ret < 0)
1443 return ret;
1444
1445 return ret ? FB_BLANK_UNBLANK : FB_BLANK_POWERDOWN;
1446}
1447
8fbea019 1448static int read_brightness_max(struct asus_wmi *asus)
3d7b1655 1449{
dfed65d5 1450 u32 retval;
d33da3b6 1451 int err;
3d7b1655 1452
1d070f89 1453 err = asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_BRIGHTNESS, &retval);
3d7b1655 1454
8fbea019
CC
1455 if (err < 0)
1456 return err;
1457
1458 retval = retval & ASUS_WMI_DSTS_MAX_BRIGTH_MASK;
1459 retval >>= 8;
1460
1461 if (!retval)
1462 return -ENODEV;
1463
1464 return retval;
1465}
1466
1467static int read_brightness(struct backlight_device *bd)
1468{
1469 struct asus_wmi *asus = bl_get_data(bd);
0986f25f
DC
1470 u32 retval;
1471 int err;
8fbea019
CC
1472
1473 err = asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_BRIGHTNESS, &retval);
1474
d33da3b6
CC
1475 if (err < 0)
1476 return err;
1477
1478 return retval & ASUS_WMI_DSTS_BRIGHTNESS_MASK;
3d7b1655
YW
1479}
1480
c87992d1 1481static u32 get_scalar_command(struct backlight_device *bd)
3d7b1655 1482{
1d070f89 1483 struct asus_wmi *asus = bl_get_data(bd);
c87992d1 1484 u32 ctrl_param = 0;
3d7b1655 1485
c87992d1
AK
1486 if ((asus->driver->brightness < bd->props.brightness) ||
1487 bd->props.brightness == bd->props.max_brightness)
1488 ctrl_param = 0x00008001;
1489 else if ((asus->driver->brightness > bd->props.brightness) ||
1490 bd->props.brightness == 0)
1491 ctrl_param = 0x00008000;
3d7b1655 1492
c87992d1 1493 asus->driver->brightness = bd->props.brightness;
3d7b1655 1494
c87992d1
AK
1495 return ctrl_param;
1496}
1497
3d7b1655
YW
1498static int update_bl_status(struct backlight_device *bd)
1499{
1d070f89 1500 struct asus_wmi *asus = bl_get_data(bd);
dfed65d5 1501 u32 ctrl_param;
6e0044be 1502 int power, err = 0;
b7187265 1503
1d070f89 1504 power = read_backlight_power(asus);
b7187265
CC
1505 if (power != -ENODEV && bd->props.power != power) {
1506 ctrl_param = !!(bd->props.power == FB_BLANK_UNBLANK);
d33da3b6
CC
1507 err = asus_wmi_set_devstate(ASUS_WMI_DEVID_BACKLIGHT,
1508 ctrl_param, NULL);
6e0044be
AK
1509 if (asus->driver->quirks->store_backlight_power)
1510 asus->driver->panel_power = bd->props.power;
6e0044be 1511
ade28abd
CC
1512 /* When using scalar brightness, updating the brightness
1513 * will mess with the backlight power */
1514 if (asus->driver->quirks->scalar_panel_brightness)
1515 return err;
b7187265 1516 }
ade28abd
CC
1517
1518 if (asus->driver->quirks->scalar_panel_brightness)
1519 ctrl_param = get_scalar_command(bd);
1520 else
1521 ctrl_param = bd->props.brightness;
1522
1523 err = asus_wmi_set_devstate(ASUS_WMI_DEVID_BRIGHTNESS,
1524 ctrl_param, NULL);
1525
8fbea019 1526 return err;
3d7b1655
YW
1527}
1528
e12e6d94 1529static const struct backlight_ops asus_wmi_bl_ops = {
3d7b1655
YW
1530 .get_brightness = read_brightness,
1531 .update_status = update_bl_status,
1532};
1533
e12e6d94 1534static int asus_wmi_backlight_notify(struct asus_wmi *asus, int code)
3d7b1655 1535{
e12e6d94 1536 struct backlight_device *bd = asus->backlight_device;
3d7b1655 1537 int old = bd->props.brightness;
b7670ed6 1538 int new = old;
3d7b1655
YW
1539
1540 if (code >= NOTIFY_BRNUP_MIN && code <= NOTIFY_BRNUP_MAX)
1541 new = code - NOTIFY_BRNUP_MIN + 1;
1542 else if (code >= NOTIFY_BRNDOWN_MIN && code <= NOTIFY_BRNDOWN_MAX)
1543 new = code - NOTIFY_BRNDOWN_MIN;
1544
1545 bd->props.brightness = new;
1546 backlight_update_status(bd);
1547 backlight_force_update(bd, BACKLIGHT_UPDATE_HOTKEY);
1548
1549 return old;
1550}
1551
e12e6d94 1552static int asus_wmi_backlight_init(struct asus_wmi *asus)
3d7b1655
YW
1553{
1554 struct backlight_device *bd;
1555 struct backlight_properties props;
b7187265
CC
1556 int max;
1557 int power;
1558
8fbea019 1559 max = read_brightness_max(asus);
86ac2735 1560 if (max < 0)
8fbea019
CC
1561 return max;
1562
1563 power = read_backlight_power(asus);
1564
b7187265
CC
1565 if (power == -ENODEV)
1566 power = FB_BLANK_UNBLANK;
8fbea019
CC
1567 else if (power < 0)
1568 return power;
3d7b1655
YW
1569
1570 memset(&props, 0, sizeof(struct backlight_properties));
60cfa098 1571 props.type = BACKLIGHT_PLATFORM;
b7187265 1572 props.max_brightness = max;
e12e6d94
CC
1573 bd = backlight_device_register(asus->driver->name,
1574 &asus->platform_device->dev, asus,
1575 &asus_wmi_bl_ops, &props);
3d7b1655
YW
1576 if (IS_ERR(bd)) {
1577 pr_err("Could not register backlight device\n");
1578 return PTR_ERR(bd);
1579 }
1580
e12e6d94 1581 asus->backlight_device = bd;
3d7b1655 1582
6e0044be
AK
1583 if (asus->driver->quirks->store_backlight_power)
1584 asus->driver->panel_power = power;
1585
3d7b1655 1586 bd->props.brightness = read_brightness(bd);
b7187265 1587 bd->props.power = power;
3d7b1655
YW
1588 backlight_update_status(bd);
1589
c87992d1
AK
1590 asus->driver->brightness = bd->props.brightness;
1591
3d7b1655
YW
1592 return 0;
1593}
1594
e12e6d94 1595static void asus_wmi_backlight_exit(struct asus_wmi *asus)
3d7b1655 1596{
00981810 1597 backlight_device_unregister(asus->backlight_device);
3d7b1655 1598
e12e6d94 1599 asus->backlight_device = NULL;
3d7b1655
YW
1600}
1601
a2a96f0c
AK
1602static int is_display_toggle(int code)
1603{
1604 /* display toggle keys */
1605 if ((code >= 0x61 && code <= 0x67) ||
1606 (code >= 0x8c && code <= 0x93) ||
1607 (code >= 0xa0 && code <= 0xa7) ||
1608 (code >= 0xd0 && code <= 0xd5))
1609 return 1;
1610
1611 return 0;
1612}
1613
487579ba
CC
1614static bool asus_wmi_has_fnlock_key(struct asus_wmi *asus)
1615{
1616 u32 result;
1617
1618 asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_FNLOCK, &result);
1619
1620 return (result & ASUS_WMI_DSTS_PRESENCE_BIT) &&
1621 !(result & ASUS_WMI_FNLOCK_BIOS_DISABLED);
1622}
1623
1624static void asus_wmi_fnlock_update(struct asus_wmi *asus)
1625{
1626 int mode = asus->fnlock_locked;
1627
1628 asus_wmi_set_devstate(ASUS_WMI_DEVID_FNLOCK, mode, NULL);
1629}
1630
e12e6d94 1631static void asus_wmi_notify(u32 value, void *context)
3d7b1655 1632{
e12e6d94 1633 struct asus_wmi *asus = context;
3d7b1655
YW
1634 struct acpi_buffer response = { ACPI_ALLOCATE_BUFFER, NULL };
1635 union acpi_object *obj;
1636 acpi_status status;
1637 int code;
1638 int orig_code;
c4453f6a
SF
1639 unsigned int key_value = 1;
1640 bool autorelease = 1;
3d7b1655
YW
1641
1642 status = wmi_get_event_data(value, &response);
1643 if (status != AE_OK) {
1644 pr_err("bad event status 0x%x\n", status);
1645 return;
1646 }
1647
1648 obj = (union acpi_object *)response.pointer;
1649
57ab7dae
CC
1650 if (!obj || obj->type != ACPI_TYPE_INTEGER)
1651 goto exit;
3d7b1655 1652
57ab7dae
CC
1653 code = obj->integer.value;
1654 orig_code = code;
3d7b1655 1655
c4453f6a
SF
1656 if (asus->driver->key_filter) {
1657 asus->driver->key_filter(asus->driver, &code, &key_value,
1658 &autorelease);
1659 if (code == ASUS_WMI_KEY_IGNORE)
1660 goto exit;
1661 }
1662
57ab7dae 1663 if (code >= NOTIFY_BRNUP_MIN && code <= NOTIFY_BRNUP_MAX)
3ba0302b 1664 code = ASUS_WMI_BRN_UP;
57ab7dae
CC
1665 else if (code >= NOTIFY_BRNDOWN_MIN &&
1666 code <= NOTIFY_BRNDOWN_MAX)
3ba0302b 1667 code = ASUS_WMI_BRN_DOWN;
3d7b1655 1668
3ba0302b 1669 if (code == ASUS_WMI_BRN_DOWN || code == ASUS_WMI_BRN_UP) {
62c4aa1a 1670 if (acpi_video_get_backlight_type() == acpi_backlight_vendor) {
57ab7dae 1671 asus_wmi_backlight_notify(asus, orig_code);
3ba0302b 1672 goto exit;
a2a96f0c 1673 }
a2a96f0c
AK
1674 }
1675
dbb3d78f 1676 if (code == NOTIFY_KBD_BRTUP) {
29f6eb53 1677 kbd_led_set_by_kbd(asus, asus->kbd_led_wk + 1);
dbb3d78f
CC
1678 goto exit;
1679 }
1680 if (code == NOTIFY_KBD_BRTDWN) {
29f6eb53 1681 kbd_led_set_by_kbd(asus, asus->kbd_led_wk - 1);
dbb3d78f
CC
1682 goto exit;
1683 }
ed99d29b
CC
1684 if (code == NOTIFY_KBD_BRTTOGGLE) {
1685 if (asus->kbd_led_wk == asus->kbd_led.max_brightness)
29f6eb53 1686 kbd_led_set_by_kbd(asus, 0);
ed99d29b 1687 else
29f6eb53 1688 kbd_led_set_by_kbd(asus, asus->kbd_led_wk + 1);
ed99d29b
CC
1689 goto exit;
1690 }
dbb3d78f 1691
487579ba
CC
1692 if (code == NOTIFY_FNLOCK_TOGGLE) {
1693 asus->fnlock_locked = !asus->fnlock_locked;
1694 asus_wmi_fnlock_update(asus);
1695 goto exit;
1696 }
1697
a2a96f0c
AK
1698 if (is_display_toggle(code) &&
1699 asus->driver->quirks->no_display_toggle)
1700 goto exit;
1701
1702 if (!sparse_keymap_report_event(asus->inputdev, code,
1703 key_value, autorelease))
57ab7dae 1704 pr_info("Unknown key %x pressed\n", code);
3d7b1655 1705
57ab7dae 1706exit:
3d7b1655
YW
1707 kfree(obj);
1708}
1709
9e1565bc
CC
1710/*
1711 * Sys helpers
1712 */
1713static int parse_arg(const char *buf, unsigned long count, int *val)
1714{
1715 if (!count)
1716 return 0;
1717 if (sscanf(buf, "%i", val) != 1)
1718 return -EINVAL;
1719 return count;
1720}
1721
1d070f89
CC
1722static ssize_t store_sys_wmi(struct asus_wmi *asus, int devid,
1723 const char *buf, size_t count)
9e1565bc 1724{
9e1565bc 1725 u32 retval;
d33da3b6 1726 int rv, err, value;
9e1565bc 1727
1d070f89 1728 value = asus_wmi_get_devstate_simple(asus, devid);
b8298340 1729 if (value < 0)
9e1565bc
CC
1730 return value;
1731
1732 rv = parse_arg(buf, count, &value);
d33da3b6
CC
1733 err = asus_wmi_set_devstate(devid, value, &retval);
1734
1735 if (err < 0)
1736 return err;
9e1565bc 1737
9e1565bc
CC
1738 return rv;
1739}
1740
1d070f89 1741static ssize_t show_sys_wmi(struct asus_wmi *asus, int devid, char *buf)
9e1565bc 1742{
1d070f89 1743 int value = asus_wmi_get_devstate_simple(asus, devid);
9e1565bc
CC
1744
1745 if (value < 0)
1746 return value;
1747
1748 return sprintf(buf, "%d\n", value);
1749}
1750
e12e6d94 1751#define ASUS_WMI_CREATE_DEVICE_ATTR(_name, _mode, _cm) \
9e1565bc
CC
1752 static ssize_t show_##_name(struct device *dev, \
1753 struct device_attribute *attr, \
1754 char *buf) \
1755 { \
1d070f89
CC
1756 struct asus_wmi *asus = dev_get_drvdata(dev); \
1757 \
1758 return show_sys_wmi(asus, _cm, buf); \
9e1565bc
CC
1759 } \
1760 static ssize_t store_##_name(struct device *dev, \
1761 struct device_attribute *attr, \
1762 const char *buf, size_t count) \
1763 { \
1d070f89
CC
1764 struct asus_wmi *asus = dev_get_drvdata(dev); \
1765 \
1766 return store_sys_wmi(asus, _cm, buf, count); \
9e1565bc
CC
1767 } \
1768 static struct device_attribute dev_attr_##_name = { \
1769 .attr = { \
1770 .name = __stringify(_name), \
1771 .mode = _mode }, \
1772 .show = show_##_name, \
1773 .store = store_##_name, \
1774 }
1775
e12e6d94
CC
1776ASUS_WMI_CREATE_DEVICE_ATTR(touchpad, 0644, ASUS_WMI_DEVID_TOUCHPAD);
1777ASUS_WMI_CREATE_DEVICE_ATTR(camera, 0644, ASUS_WMI_DEVID_CAMERA);
1778ASUS_WMI_CREATE_DEVICE_ATTR(cardr, 0644, ASUS_WMI_DEVID_CARDREADER);
c0b91b6d 1779ASUS_WMI_CREATE_DEVICE_ATTR(lid_resume, 0644, ASUS_WMI_DEVID_LID_RESUME);
aca234f6 1780ASUS_WMI_CREATE_DEVICE_ATTR(als_enable, 0644, ASUS_WMI_DEVID_ALS_ENABLE);
9e1565bc 1781
a8fe3428 1782static ssize_t cpufv_store(struct device *dev, struct device_attribute *attr,
67fa38ec 1783 const char *buf, size_t count)
7f80d734 1784{
3df5fdad 1785 int value, rv;
7f80d734
CB
1786
1787 if (!count || sscanf(buf, "%i", &value) != 1)
1788 return -EINVAL;
1789 if (value < 0 || value > 2)
1790 return -EINVAL;
1791
3df5fdad
CC
1792 rv = asus_wmi_evaluate_method(ASUS_WMI_METHODID_CFVS, value, 0, NULL);
1793 if (rv < 0)
1794 return rv;
1795
1796 return count;
7f80d734
CB
1797}
1798
a8fe3428 1799static DEVICE_ATTR_WO(cpufv);
7f80d734 1800
4e37b42d
CC
1801static struct attribute *platform_attributes[] = {
1802 &dev_attr_cpufv.attr,
9e1565bc
CC
1803 &dev_attr_camera.attr,
1804 &dev_attr_cardr.attr,
4615bb66 1805 &dev_attr_touchpad.attr,
c0b91b6d 1806 &dev_attr_lid_resume.attr,
aca234f6 1807 &dev_attr_als_enable.attr,
4e37b42d
CC
1808 NULL
1809};
1810
587a1f16 1811static umode_t asus_sysfs_is_visible(struct kobject *kobj,
e12e6d94 1812 struct attribute *attr, int idx)
9e1565bc 1813{
1d070f89 1814 struct device *dev = container_of(kobj, struct device, kobj);
d605ca29 1815 struct asus_wmi *asus = dev_get_drvdata(dev);
1d070f89 1816 bool ok = true;
9e1565bc
CC
1817 int devid = -1;
1818
1819 if (attr == &dev_attr_camera.attr)
e12e6d94 1820 devid = ASUS_WMI_DEVID_CAMERA;
9e1565bc 1821 else if (attr == &dev_attr_cardr.attr)
e12e6d94 1822 devid = ASUS_WMI_DEVID_CARDREADER;
4615bb66 1823 else if (attr == &dev_attr_touchpad.attr)
e12e6d94 1824 devid = ASUS_WMI_DEVID_TOUCHPAD;
c0b91b6d
AK
1825 else if (attr == &dev_attr_lid_resume.attr)
1826 devid = ASUS_WMI_DEVID_LID_RESUME;
aca234f6
OR
1827 else if (attr == &dev_attr_als_enable.attr)
1828 devid = ASUS_WMI_DEVID_ALS_ENABLE;
9e1565bc
CC
1829
1830 if (devid != -1)
1d070f89 1831 ok = !(asus_wmi_get_devstate_simple(asus, devid) < 0);
9e1565bc 1832
1d070f89 1833 return ok ? attr->mode : 0;
9e1565bc
CC
1834}
1835
e90d9ba8 1836static const struct attribute_group platform_attribute_group = {
e12e6d94
CC
1837 .is_visible = asus_sysfs_is_visible,
1838 .attrs = platform_attributes
4e37b42d
CC
1839};
1840
e12e6d94 1841static void asus_wmi_sysfs_exit(struct platform_device *device)
7f80d734 1842{
4e37b42d 1843 sysfs_remove_group(&device->dev.kobj, &platform_attribute_group);
7f80d734
CB
1844}
1845
e12e6d94 1846static int asus_wmi_sysfs_init(struct platform_device *device)
7f80d734 1847{
4e37b42d 1848 return sysfs_create_group(&device->dev.kobj, &platform_attribute_group);
7f80d734
CB
1849}
1850
27c136c8
CC
1851/*
1852 * Platform device
1853 */
39ddf3bf 1854static int asus_wmi_platform_init(struct asus_wmi *asus)
ee027e4a 1855{
46dbca87
CC
1856 int rv;
1857
1858 /* INIT enable hotkeys on some models */
1859 if (!asus_wmi_evaluate_method(ASUS_WMI_METHODID_INIT, 0, 0, &rv))
0ed60654 1860 pr_info("Initialization: %#x\n", rv);
46dbca87
CC
1861
1862 /* We don't know yet what to do with this version... */
1863 if (!asus_wmi_evaluate_method(ASUS_WMI_METHODID_SPEC, 0, 0x9, &rv)) {
0ed60654 1864 pr_info("BIOS WMI version: %d.%d\n", rv >> 16, rv & 0xFF);
46dbca87
CC
1865 asus->spec = rv;
1866 }
1867
1868 /*
1869 * The SFUN method probably allows the original driver to get the list
1870 * of features supported by a given model. For now, 0x0100 or 0x0800
1871 * bit signifies that the laptop is equipped with a Wi-Fi MiniPCI card.
1872 * The significance of others is yet to be found.
1873 */
1874 if (!asus_wmi_evaluate_method(ASUS_WMI_METHODID_SFUN, 0, 0, &rv)) {
0ed60654 1875 pr_info("SFUN value: %#x\n", rv);
46dbca87
CC
1876 asus->sfun = rv;
1877 }
1878
1d070f89
CC
1879 /*
1880 * Eee PC and Notebooks seems to have different method_id for DSTS,
1881 * but it may also be related to the BIOS's SPEC.
1882 * Note, on most Eeepc, there is no way to check if a method exist
1883 * or note, while on notebooks, they returns 0xFFFFFFFE on failure,
1884 * but once again, SPEC may probably be used for that kind of things.
1885 */
1886 if (!asus_wmi_evaluate_method(ASUS_WMI_METHODID_DSTS, 0, 0, NULL))
1887 asus->dsts_id = ASUS_WMI_METHODID_DSTS;
63a78bb1 1888 else
1d070f89
CC
1889 asus->dsts_id = ASUS_WMI_METHODID_DSTS2;
1890
fddbfed5
CC
1891 /* CWAP allow to define the behavior of the Fn+F2 key,
1892 * this method doesn't seems to be present on Eee PCs */
6a2bcccd 1893 if (asus->driver->quirks->wapf >= 0)
fddbfed5 1894 asus_wmi_set_devstate(ASUS_WMI_DEVID_CWAP,
6a2bcccd 1895 asus->driver->quirks->wapf, NULL);
fddbfed5 1896
e12e6d94 1897 return asus_wmi_sysfs_init(asus->platform_device);
27c136c8
CC
1898}
1899
e12e6d94 1900static void asus_wmi_platform_exit(struct asus_wmi *asus)
27c136c8 1901{
e12e6d94 1902 asus_wmi_sysfs_exit(asus->platform_device);
27c136c8
CC
1903}
1904
8c1b2d83
CC
1905/*
1906 * debugfs
1907 */
e12e6d94
CC
1908struct asus_wmi_debugfs_node {
1909 struct asus_wmi *asus;
8c1b2d83 1910 char *name;
e12e6d94 1911 int (*show) (struct seq_file *m, void *data);
8c1b2d83
CC
1912};
1913
1914static int show_dsts(struct seq_file *m, void *data)
1915{
e12e6d94 1916 struct asus_wmi *asus = m->private;
d33da3b6 1917 int err;
8c1b2d83
CC
1918 u32 retval = -1;
1919
1d070f89 1920 err = asus_wmi_get_devstate(asus, asus->debug.dev_id, &retval);
8c1b2d83 1921
d33da3b6
CC
1922 if (err < 0)
1923 return err;
8c1b2d83 1924
ef343491 1925 seq_printf(m, "DSTS(%#x) = %#x\n", asus->debug.dev_id, retval);
8c1b2d83
CC
1926
1927 return 0;
1928}
1929
1930static int show_devs(struct seq_file *m, void *data)
1931{
e12e6d94 1932 struct asus_wmi *asus = m->private;
d33da3b6 1933 int err;
8c1b2d83
CC
1934 u32 retval = -1;
1935
d33da3b6
CC
1936 err = asus_wmi_set_devstate(asus->debug.dev_id, asus->debug.ctrl_param,
1937 &retval);
1938
1939 if (err < 0)
1940 return err;
8c1b2d83 1941
ef343491 1942 seq_printf(m, "DEVS(%#x, %#x) = %#x\n", asus->debug.dev_id,
e12e6d94 1943 asus->debug.ctrl_param, retval);
8c1b2d83
CC
1944
1945 return 0;
1946}
1947
ef343491
CC
1948static int show_call(struct seq_file *m, void *data)
1949{
1950 struct asus_wmi *asus = m->private;
1951 struct bios_args args = {
1952 .arg0 = asus->debug.dev_id,
1953 .arg1 = asus->debug.ctrl_param,
1954 };
1955 struct acpi_buffer input = { (acpi_size) sizeof(args), &args };
1956 struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER, NULL };
1957 union acpi_object *obj;
1958 acpi_status status;
1959
1960 status = wmi_evaluate_method(ASUS_WMI_MGMT_GUID,
0fe57261 1961 0, asus->debug.method_id,
ef343491
CC
1962 &input, &output);
1963
1964 if (ACPI_FAILURE(status))
1965 return -EIO;
1966
1967 obj = (union acpi_object *)output.pointer;
1968 if (obj && obj->type == ACPI_TYPE_INTEGER)
1969 seq_printf(m, "%#x(%#x, %#x) = %#x\n", asus->debug.method_id,
1970 asus->debug.dev_id, asus->debug.ctrl_param,
1971 (u32) obj->integer.value);
1972 else
1973 seq_printf(m, "%#x(%#x, %#x) = t:%d\n", asus->debug.method_id,
1974 asus->debug.dev_id, asus->debug.ctrl_param,
a1d60867 1975 obj ? obj->type : -1);
ef343491
CC
1976
1977 kfree(obj);
1978
1979 return 0;
1980}
1981
e12e6d94
CC
1982static struct asus_wmi_debugfs_node asus_wmi_debug_files[] = {
1983 {NULL, "devs", show_devs},
1984 {NULL, "dsts", show_dsts},
ef343491 1985 {NULL, "call", show_call},
8c1b2d83
CC
1986};
1987
e12e6d94 1988static int asus_wmi_debugfs_open(struct inode *inode, struct file *file)
8c1b2d83 1989{
e12e6d94 1990 struct asus_wmi_debugfs_node *node = inode->i_private;
8c1b2d83 1991
e12e6d94 1992 return single_open(file, node->show, node->asus);
8c1b2d83
CC
1993}
1994
e12e6d94 1995static const struct file_operations asus_wmi_debugfs_io_ops = {
8c1b2d83 1996 .owner = THIS_MODULE,
e12e6d94 1997 .open = asus_wmi_debugfs_open,
8c1b2d83
CC
1998 .read = seq_read,
1999 .llseek = seq_lseek,
2000 .release = single_release,
2001};
2002
e12e6d94 2003static void asus_wmi_debugfs_exit(struct asus_wmi *asus)
8c1b2d83 2004{
e12e6d94 2005 debugfs_remove_recursive(asus->debug.root);
8c1b2d83
CC
2006}
2007
e12e6d94 2008static int asus_wmi_debugfs_init(struct asus_wmi *asus)
8c1b2d83
CC
2009{
2010 struct dentry *dent;
2011 int i;
2012
e12e6d94
CC
2013 asus->debug.root = debugfs_create_dir(asus->driver->name, NULL);
2014 if (!asus->debug.root) {
0ed60654 2015 pr_err("failed to create debugfs directory\n");
8c1b2d83
CC
2016 goto error_debugfs;
2017 }
2018
ef343491
CC
2019 dent = debugfs_create_x32("method_id", S_IRUGO | S_IWUSR,
2020 asus->debug.root, &asus->debug.method_id);
2021 if (!dent)
2022 goto error_debugfs;
2023
e12e6d94
CC
2024 dent = debugfs_create_x32("dev_id", S_IRUGO | S_IWUSR,
2025 asus->debug.root, &asus->debug.dev_id);
8c1b2d83
CC
2026 if (!dent)
2027 goto error_debugfs;
2028
e12e6d94
CC
2029 dent = debugfs_create_x32("ctrl_param", S_IRUGO | S_IWUSR,
2030 asus->debug.root, &asus->debug.ctrl_param);
8c1b2d83
CC
2031 if (!dent)
2032 goto error_debugfs;
2033
e12e6d94
CC
2034 for (i = 0; i < ARRAY_SIZE(asus_wmi_debug_files); i++) {
2035 struct asus_wmi_debugfs_node *node = &asus_wmi_debug_files[i];
8c1b2d83 2036
e12e6d94 2037 node->asus = asus;
8c1b2d83 2038 dent = debugfs_create_file(node->name, S_IFREG | S_IRUGO,
e12e6d94
CC
2039 asus->debug.root, node,
2040 &asus_wmi_debugfs_io_ops);
8c1b2d83
CC
2041 if (!dent) {
2042 pr_err("failed to create debug file: %s\n", node->name);
2043 goto error_debugfs;
2044 }
2045 }
2046
2047 return 0;
2048
2049error_debugfs:
e12e6d94 2050 asus_wmi_debugfs_exit(asus);
8c1b2d83
CC
2051 return -ENOMEM;
2052}
2053
53e755c2
KB
2054static int asus_wmi_fan_init(struct asus_wmi *asus)
2055{
2056 int status;
2057
2058 asus->asus_hwmon_pwm = -1;
2059 asus->asus_hwmon_num_fans = -1;
2060 asus->asus_hwmon_fan_manual_mode = false;
2061
2062 status = asus_hwmon_get_fan_number(asus, &asus->asus_hwmon_num_fans);
2063 if (status) {
2064 asus->asus_hwmon_num_fans = 0;
2065 pr_warn("Could not determine number of fans: %d\n", status);
2066 return -ENXIO;
2067 }
2068
2069 pr_info("Number of fans: %d\n", asus->asus_hwmon_num_fans);
2070 return 0;
2071}
2072
27c136c8
CC
2073/*
2074 * WMI Driver
2075 */
e12e6d94 2076static int asus_wmi_add(struct platform_device *pdev)
afa7c886 2077{
e12e6d94
CC
2078 struct platform_driver *pdrv = to_platform_driver(pdev->dev.driver);
2079 struct asus_wmi_driver *wdrv = to_asus_wmi_driver(pdrv);
2080 struct asus_wmi *asus;
d8c66f62 2081 const char *chassis_type;
ee027e4a 2082 acpi_status status;
27c136c8 2083 int err;
a50bd128 2084 u32 result;
ee027e4a 2085
e12e6d94
CC
2086 asus = kzalloc(sizeof(struct asus_wmi), GFP_KERNEL);
2087 if (!asus)
a04ce290
CC
2088 return -ENOMEM;
2089
e12e6d94
CC
2090 asus->driver = wdrv;
2091 asus->platform_device = pdev;
2092 wdrv->platform_device = pdev;
2093 platform_set_drvdata(asus->platform_device, asus);
27c136c8 2094
c87992d1
AK
2095 if (wdrv->detect_quirks)
2096 wdrv->detect_quirks(asus->driver);
afa7c886 2097
e12e6d94 2098 err = asus_wmi_platform_init(asus);
27c136c8
CC
2099 if (err)
2100 goto fail_platform;
45f2c693 2101
e12e6d94 2102 err = asus_wmi_input_init(asus);
45f2c693 2103 if (err)
27c136c8 2104 goto fail_input;
3d7b1655 2105
53e755c2
KB
2106 err = asus_wmi_fan_init(asus); /* probably no problems on error */
2107 asus_hwmon_fan_set_auto(asus);
2108
e07babde
CC
2109 err = asus_wmi_hwmon_init(asus);
2110 if (err)
2111 goto fail_hwmon;
2112
e12e6d94 2113 err = asus_wmi_led_init(asus);
084fca63
CC
2114 if (err)
2115 goto fail_leds;
2116
71050ae7
JPRV
2117 asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_WLAN, &result);
2118 if (result & (ASUS_WMI_DSTS_PRESENCE_BIT | ASUS_WMI_DSTS_USER_BIT))
2119 asus->driver->wlan_ctrl_by_user = 1;
2120
d1c4e9bf 2121 if (!(asus->driver->wlan_ctrl_by_user && ashs_present())) {
a977e59c
JPRV
2122 err = asus_wmi_rfkill_init(asus);
2123 if (err)
2124 goto fail_rfkill;
2125 }
ba48fdb9 2126
e9b61518
OR
2127 if (asus->driver->quirks->wmi_force_als_set)
2128 asus_wmi_set_als();
2129
d8c66f62
HG
2130 /* Some Asus desktop boards export an acpi-video backlight interface,
2131 stop this from showing up */
2132 chassis_type = dmi_get_system_info(DMI_CHASSIS_TYPE);
2133 if (chassis_type && !strcmp(chassis_type, "3"))
62c4aa1a
HG
2134 acpi_video_set_dmi_backlight_type(acpi_backlight_vendor);
2135
272c77d5 2136 if (asus->driver->quirks->wmi_backlight_power)
62c4aa1a
HG
2137 acpi_video_set_dmi_backlight_type(acpi_backlight_vendor);
2138
999d4376 2139 if (asus->driver->quirks->wmi_backlight_native)
2140 acpi_video_set_dmi_backlight_type(acpi_backlight_native);
2141
8023eff1
KCH
2142 if (asus->driver->quirks->xusb2pr)
2143 asus_wmi_set_xusb2pr(asus);
2144
62c4aa1a 2145 if (acpi_video_get_backlight_type() == acpi_backlight_vendor) {
e12e6d94 2146 err = asus_wmi_backlight_init(asus);
b7187265 2147 if (err && err != -ENODEV)
27c136c8 2148 goto fail_backlight;
78f3ac76
JPRV
2149 } else
2150 err = asus_wmi_set_devstate(ASUS_WMI_DEVID_BACKLIGHT, 2, NULL);
45f2c693 2151
487579ba
CC
2152 if (asus_wmi_has_fnlock_key(asus)) {
2153 asus->fnlock_locked = true;
2154 asus_wmi_fnlock_update(asus);
2155 }
2156
e12e6d94
CC
2157 status = wmi_install_notify_handler(asus->driver->event_guid,
2158 asus_wmi_notify, asus);
45f2c693 2159 if (ACPI_FAILURE(status)) {
e12e6d94 2160 pr_err("Unable to register notify handler - %d\n", status);
45f2c693 2161 err = -ENODEV;
27c136c8 2162 goto fail_wmi_handler;
45f2c693
YW
2163 }
2164
e12e6d94 2165 err = asus_wmi_debugfs_init(asus);
8c1b2d83
CC
2166 if (err)
2167 goto fail_debugfs;
2168
a04ce290 2169 return 0;
45f2c693 2170
8c1b2d83 2171fail_debugfs:
e12e6d94 2172 wmi_remove_notify_handler(asus->driver->event_guid);
27c136c8 2173fail_wmi_handler:
e12e6d94 2174 asus_wmi_backlight_exit(asus);
27c136c8 2175fail_backlight:
e12e6d94 2176 asus_wmi_rfkill_exit(asus);
ba48fdb9 2177fail_rfkill:
e12e6d94 2178 asus_wmi_led_exit(asus);
084fca63 2179fail_leds:
e07babde 2180fail_hwmon:
e12e6d94 2181 asus_wmi_input_exit(asus);
27c136c8 2182fail_input:
e12e6d94 2183 asus_wmi_platform_exit(asus);
27c136c8 2184fail_platform:
e12e6d94 2185 kfree(asus);
a04ce290 2186 return err;
45f2c693
YW
2187}
2188
e12e6d94 2189static int asus_wmi_remove(struct platform_device *device)
45f2c693 2190{
e12e6d94
CC
2191 struct asus_wmi *asus;
2192
2193 asus = platform_get_drvdata(device);
2194 wmi_remove_notify_handler(asus->driver->event_guid);
2195 asus_wmi_backlight_exit(asus);
2196 asus_wmi_input_exit(asus);
2197 asus_wmi_led_exit(asus);
2198 asus_wmi_rfkill_exit(asus);
2199 asus_wmi_debugfs_exit(asus);
2200 asus_wmi_platform_exit(asus);
53e755c2 2201 asus_hwmon_fan_set_auto(asus);
e12e6d94
CC
2202
2203 kfree(asus);
45f2c693
YW
2204 return 0;
2205}
2206
0773d7f9
CC
2207/*
2208 * Platform driver - hibernate/resume callbacks
2209 */
e12e6d94 2210static int asus_hotk_thaw(struct device *device)
0773d7f9 2211{
e12e6d94 2212 struct asus_wmi *asus = dev_get_drvdata(device);
0773d7f9 2213
a7ce3f04 2214 if (asus->wlan.rfkill) {
0773d7f9
CC
2215 bool wlan;
2216
2217 /*
2218 * Work around bios bug - acpi _PTS turns off the wireless led
2219 * during suspend. Normally it restores it on resume, but
2220 * we should kick it ourselves in case hibernation is aborted.
2221 */
1d070f89 2222 wlan = asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_WLAN);
e12e6d94 2223 asus_wmi_set_devstate(ASUS_WMI_DEVID_WLAN, wlan, NULL);
0773d7f9
CC
2224 }
2225
2226 return 0;
2227}
2228
30734049
OR
2229static int asus_hotk_resume(struct device *device)
2230{
2231 struct asus_wmi *asus = dev_get_drvdata(device);
2232
2233 if (!IS_ERR_OR_NULL(asus->kbd_led.dev))
9fe44fc9 2234 kbd_led_update(asus);
30734049 2235
487579ba
CC
2236 if (asus_wmi_has_fnlock_key(asus))
2237 asus_wmi_fnlock_update(asus);
30734049
OR
2238 return 0;
2239}
2240
e12e6d94 2241static int asus_hotk_restore(struct device *device)
0773d7f9 2242{
e12e6d94 2243 struct asus_wmi *asus = dev_get_drvdata(device);
0773d7f9
CC
2244 int bl;
2245
2246 /* Refresh both wlan rfkill state and pci hotplug */
a7ce3f04 2247 if (asus->wlan.rfkill)
e12e6d94 2248 asus_rfkill_hotplug(asus);
0773d7f9 2249
a7ce3f04 2250 if (asus->bluetooth.rfkill) {
1d070f89
CC
2251 bl = !asus_wmi_get_devstate_simple(asus,
2252 ASUS_WMI_DEVID_BLUETOOTH);
a7ce3f04 2253 rfkill_set_sw_state(asus->bluetooth.rfkill, bl);
2e9e159d 2254 }
a7ce3f04 2255 if (asus->wimax.rfkill) {
1d070f89 2256 bl = !asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_WIMAX);
a7ce3f04 2257 rfkill_set_sw_state(asus->wimax.rfkill, bl);
2e9e159d 2258 }
a7ce3f04 2259 if (asus->wwan3g.rfkill) {
1d070f89 2260 bl = !asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_WWAN3G);
a7ce3f04 2261 rfkill_set_sw_state(asus->wwan3g.rfkill, bl);
0773d7f9 2262 }
43be8bde
CC
2263 if (asus->gps.rfkill) {
2264 bl = !asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_GPS);
2265 rfkill_set_sw_state(asus->gps.rfkill, bl);
2266 }
a912d329
CC
2267 if (asus->uwb.rfkill) {
2268 bl = !asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_UWB);
2269 rfkill_set_sw_state(asus->uwb.rfkill, bl);
2270 }
30734049 2271 if (!IS_ERR_OR_NULL(asus->kbd_led.dev))
9fe44fc9 2272 kbd_led_update(asus);
0773d7f9 2273
487579ba
CC
2274 if (asus_wmi_has_fnlock_key(asus))
2275 asus_wmi_fnlock_update(asus);
0773d7f9
CC
2276 return 0;
2277}
2278
e12e6d94
CC
2279static const struct dev_pm_ops asus_pm_ops = {
2280 .thaw = asus_hotk_thaw,
2281 .restore = asus_hotk_restore,
30734049 2282 .resume = asus_hotk_resume,
45f2c693
YW
2283};
2284
e12e6d94 2285static int asus_wmi_probe(struct platform_device *pdev)
d358cb55 2286{
e12e6d94
CC
2287 struct platform_driver *pdrv = to_platform_driver(pdev->dev.driver);
2288 struct asus_wmi_driver *wdrv = to_asus_wmi_driver(pdrv);
2289 int ret;
d358cb55 2290
e12e6d94 2291 if (!wmi_has_guid(ASUS_WMI_MGMT_GUID)) {
c994611a 2292 pr_warn("ASUS Management GUID not found\n");
ee027e4a
YW
2293 return -ENODEV;
2294 }
2295
e12e6d94 2296 if (wdrv->event_guid && !wmi_has_guid(wdrv->event_guid)) {
c994611a 2297 pr_warn("ASUS Event GUID not found\n");
d358cb55
CC
2298 return -ENODEV;
2299 }
2300
e12e6d94
CC
2301 if (wdrv->probe) {
2302 ret = wdrv->probe(pdev);
2303 if (ret)
2304 return ret;
2305 }
2306
2307 return asus_wmi_add(pdev);
a04ce290 2308}
45f2c693 2309
e12e6d94 2310static bool used;
ee027e4a 2311
8fe8c25e 2312int __init_or_module asus_wmi_register_driver(struct asus_wmi_driver *driver)
a04ce290 2313{
e12e6d94
CC
2314 struct platform_driver *platform_driver;
2315 struct platform_device *platform_device;
2316
2317 if (used)
2318 return -EBUSY;
2319
2320 platform_driver = &driver->platform_driver;
2321 platform_driver->remove = asus_wmi_remove;
2322 platform_driver->driver.owner = driver->owner;
2323 platform_driver->driver.name = driver->name;
2324 platform_driver->driver.pm = &asus_pm_ops;
2325
2326 platform_device = platform_create_bundle(platform_driver,
2327 asus_wmi_probe,
a04ce290
CC
2328 NULL, 0, NULL, 0);
2329 if (IS_ERR(platform_device))
2330 return PTR_ERR(platform_device);
e12e6d94
CC
2331
2332 used = true;
2333 return 0;
2334}
2335EXPORT_SYMBOL_GPL(asus_wmi_register_driver);
2336
2337void asus_wmi_unregister_driver(struct asus_wmi_driver *driver)
2338{
2339 platform_device_unregister(driver->platform_device);
2340 platform_driver_unregister(&driver->platform_driver);
2341 used = false;
2342}
2343EXPORT_SYMBOL_GPL(asus_wmi_unregister_driver);
2344
2345static int __init asus_wmi_init(void)
2346{
0ed60654 2347 pr_info("ASUS WMI generic driver loaded\n");
ee027e4a
YW
2348 return 0;
2349}
2350
e12e6d94 2351static void __exit asus_wmi_exit(void)
ee027e4a 2352{
0ed60654 2353 pr_info("ASUS WMI generic driver unloaded\n");
ee027e4a
YW
2354}
2355
e12e6d94
CC
2356module_init(asus_wmi_init);
2357module_exit(asus_wmi_exit);