Loading...
1/*
2 * Asus PC WMI hotkey driver
3 *
4 * Copyright(C) 2010 Intel Corporation.
5 * Copyright(C) 2010-2011 Corentin Chary <corentin.chary@gmail.com>
6 *
7 * Portions based on wistron_btns.c:
8 * Copyright (C) 2005 Miloslav Trmac <mitr@volny.cz>
9 * Copyright (C) 2005 Bernhard Rosenkraenzer <bero@arklinux.org>
10 * Copyright (C) 2005 Dmitry Torokhov <dtor@mail.ru>
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 */
26
27#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
28
29#include <linux/kernel.h>
30#include <linux/module.h>
31#include <linux/init.h>
32#include <linux/types.h>
33#include <linux/slab.h>
34#include <linux/input.h>
35#include <linux/input/sparse-keymap.h>
36#include <linux/fb.h>
37#include <linux/backlight.h>
38#include <linux/leds.h>
39#include <linux/rfkill.h>
40#include <linux/pci.h>
41#include <linux/pci_hotplug.h>
42#include <linux/hwmon.h>
43#include <linux/hwmon-sysfs.h>
44#include <linux/debugfs.h>
45#include <linux/seq_file.h>
46#include <linux/platform_device.h>
47#include <linux/thermal.h>
48#include <linux/acpi.h>
49#include <linux/dmi.h>
50#include <acpi/video.h>
51
52#include "asus-wmi.h"
53
54MODULE_AUTHOR("Corentin Chary <corentin.chary@gmail.com>, "
55 "Yong Wang <yong.y.wang@intel.com>");
56MODULE_DESCRIPTION("Asus Generic WMI Driver");
57MODULE_LICENSE("GPL");
58
59#define to_asus_wmi_driver(pdrv) \
60 (container_of((pdrv), struct asus_wmi_driver, platform_driver))
61
62#define ASUS_WMI_MGMT_GUID "97845ED0-4E6D-11DE-8A39-0800200C9A66"
63
64#define NOTIFY_BRNUP_MIN 0x11
65#define NOTIFY_BRNUP_MAX 0x1f
66#define NOTIFY_BRNDOWN_MIN 0x20
67#define NOTIFY_BRNDOWN_MAX 0x2e
68#define NOTIFY_KBD_BRTUP 0xc4
69#define NOTIFY_KBD_BRTDWN 0xc5
70
71/* WMI Methods */
72#define ASUS_WMI_METHODID_SPEC 0x43455053 /* BIOS SPECification */
73#define ASUS_WMI_METHODID_SFBD 0x44424653 /* Set First Boot Device */
74#define ASUS_WMI_METHODID_GLCD 0x44434C47 /* Get LCD status */
75#define ASUS_WMI_METHODID_GPID 0x44495047 /* Get Panel ID?? (Resol) */
76#define ASUS_WMI_METHODID_QMOD 0x444F4D51 /* Quiet MODe */
77#define ASUS_WMI_METHODID_SPLV 0x4C425053 /* Set Panel Light Value */
78#define ASUS_WMI_METHODID_AGFN 0x4E464741 /* FaN? */
79#define ASUS_WMI_METHODID_SFUN 0x4E554653 /* FUNCtionalities */
80#define ASUS_WMI_METHODID_SDSP 0x50534453 /* Set DiSPlay output */
81#define ASUS_WMI_METHODID_GDSP 0x50534447 /* Get DiSPlay output */
82#define ASUS_WMI_METHODID_DEVP 0x50564544 /* DEVice Policy */
83#define ASUS_WMI_METHODID_OSVR 0x5256534F /* OS VeRsion */
84#define ASUS_WMI_METHODID_DSTS 0x53544344 /* Device STatuS */
85#define ASUS_WMI_METHODID_DSTS2 0x53545344 /* Device STatuS #2*/
86#define ASUS_WMI_METHODID_BSTS 0x53545342 /* Bios STatuS ? */
87#define ASUS_WMI_METHODID_DEVS 0x53564544 /* DEVice Set */
88#define ASUS_WMI_METHODID_CFVS 0x53564643 /* CPU Frequency Volt Set */
89#define ASUS_WMI_METHODID_KBFT 0x5446424B /* KeyBoard FilTer */
90#define ASUS_WMI_METHODID_INIT 0x54494E49 /* INITialize */
91#define ASUS_WMI_METHODID_HKEY 0x59454B48 /* Hot KEY ?? */
92
93#define ASUS_WMI_UNSUPPORTED_METHOD 0xFFFFFFFE
94
95/* Wireless */
96#define ASUS_WMI_DEVID_HW_SWITCH 0x00010001
97#define ASUS_WMI_DEVID_WIRELESS_LED 0x00010002
98#define ASUS_WMI_DEVID_CWAP 0x00010003
99#define ASUS_WMI_DEVID_WLAN 0x00010011
100#define ASUS_WMI_DEVID_WLAN_LED 0x00010012
101#define ASUS_WMI_DEVID_BLUETOOTH 0x00010013
102#define ASUS_WMI_DEVID_GPS 0x00010015
103#define ASUS_WMI_DEVID_WIMAX 0x00010017
104#define ASUS_WMI_DEVID_WWAN3G 0x00010019
105#define ASUS_WMI_DEVID_UWB 0x00010021
106
107/* Leds */
108/* 0x000200XX and 0x000400XX */
109#define ASUS_WMI_DEVID_LED1 0x00020011
110#define ASUS_WMI_DEVID_LED2 0x00020012
111#define ASUS_WMI_DEVID_LED3 0x00020013
112#define ASUS_WMI_DEVID_LED4 0x00020014
113#define ASUS_WMI_DEVID_LED5 0x00020015
114#define ASUS_WMI_DEVID_LED6 0x00020016
115
116/* Backlight and Brightness */
117#define ASUS_WMI_DEVID_BACKLIGHT 0x00050011
118#define ASUS_WMI_DEVID_BRIGHTNESS 0x00050012
119#define ASUS_WMI_DEVID_KBD_BACKLIGHT 0x00050021
120#define ASUS_WMI_DEVID_LIGHT_SENSOR 0x00050022 /* ?? */
121
122/* Misc */
123#define ASUS_WMI_DEVID_CAMERA 0x00060013
124
125/* Storage */
126#define ASUS_WMI_DEVID_CARDREADER 0x00080013
127
128/* Input */
129#define ASUS_WMI_DEVID_TOUCHPAD 0x00100011
130#define ASUS_WMI_DEVID_TOUCHPAD_LED 0x00100012
131
132/* Fan, Thermal */
133#define ASUS_WMI_DEVID_THERMAL_CTRL 0x00110011
134#define ASUS_WMI_DEVID_FAN_CTRL 0x00110012
135
136/* Power */
137#define ASUS_WMI_DEVID_PROCESSOR_STATE 0x00120012
138
139/* Deep S3 / Resume on LID open */
140#define ASUS_WMI_DEVID_LID_RESUME 0x00120031
141
142/* DSTS masks */
143#define ASUS_WMI_DSTS_STATUS_BIT 0x00000001
144#define ASUS_WMI_DSTS_UNKNOWN_BIT 0x00000002
145#define ASUS_WMI_DSTS_PRESENCE_BIT 0x00010000
146#define ASUS_WMI_DSTS_USER_BIT 0x00020000
147#define ASUS_WMI_DSTS_BIOS_BIT 0x00040000
148#define ASUS_WMI_DSTS_BRIGHTNESS_MASK 0x000000FF
149#define ASUS_WMI_DSTS_MAX_BRIGTH_MASK 0x0000FF00
150
151#define ASUS_FAN_DESC "cpu_fan"
152#define ASUS_FAN_MFUN 0x13
153#define ASUS_FAN_SFUN_READ 0x06
154#define ASUS_FAN_SFUN_WRITE 0x07
155#define ASUS_FAN_CTRL_MANUAL 1
156#define ASUS_FAN_CTRL_AUTO 2
157
158struct bios_args {
159 u32 arg0;
160 u32 arg1;
161} __packed;
162
163/*
164 * Struct that's used for all methods called via AGFN. Naming is
165 * identically to the AML code.
166 */
167struct agfn_args {
168 u16 mfun; /* probably "Multi-function" to be called */
169 u16 sfun; /* probably "Sub-function" to be called */
170 u16 len; /* size of the hole struct, including subfunction fields */
171 u8 stas; /* not used by now */
172 u8 err; /* zero on success */
173} __packed;
174
175/* struct used for calling fan read and write methods */
176struct fan_args {
177 struct agfn_args agfn; /* common fields */
178 u8 fan; /* fan number: 0: set auto mode 1: 1st fan */
179 u32 speed; /* read: RPM/100 - write: 0-255 */
180} __packed;
181
182/*
183 * <platform>/ - debugfs root directory
184 * dev_id - current dev_id
185 * ctrl_param - current ctrl_param
186 * method_id - current method_id
187 * devs - call DEVS(dev_id, ctrl_param) and print result
188 * dsts - call DSTS(dev_id) and print result
189 * call - call method_id(dev_id, ctrl_param) and print result
190 */
191struct asus_wmi_debug {
192 struct dentry *root;
193 u32 method_id;
194 u32 dev_id;
195 u32 ctrl_param;
196};
197
198struct asus_rfkill {
199 struct asus_wmi *asus;
200 struct rfkill *rfkill;
201 u32 dev_id;
202};
203
204struct asus_wmi {
205 int dsts_id;
206 int spec;
207 int sfun;
208
209 struct input_dev *inputdev;
210 struct backlight_device *backlight_device;
211 struct platform_device *platform_device;
212
213 struct led_classdev wlan_led;
214 int wlan_led_wk;
215 struct led_classdev tpd_led;
216 int tpd_led_wk;
217 struct led_classdev kbd_led;
218 int kbd_led_wk;
219 struct workqueue_struct *led_workqueue;
220 struct work_struct tpd_led_work;
221 struct work_struct kbd_led_work;
222 struct work_struct wlan_led_work;
223
224 struct asus_rfkill wlan;
225 struct asus_rfkill bluetooth;
226 struct asus_rfkill wimax;
227 struct asus_rfkill wwan3g;
228 struct asus_rfkill gps;
229 struct asus_rfkill uwb;
230
231 bool asus_hwmon_fan_manual_mode;
232 int asus_hwmon_num_fans;
233 int asus_hwmon_pwm;
234
235 struct hotplug_slot *hotplug_slot;
236 struct mutex hotplug_lock;
237 struct mutex wmi_lock;
238 struct workqueue_struct *hotplug_workqueue;
239 struct work_struct hotplug_work;
240
241 struct asus_wmi_debug debug;
242
243 struct asus_wmi_driver *driver;
244};
245
246static int asus_wmi_input_init(struct asus_wmi *asus)
247{
248 int err;
249
250 asus->inputdev = input_allocate_device();
251 if (!asus->inputdev)
252 return -ENOMEM;
253
254 asus->inputdev->name = asus->driver->input_name;
255 asus->inputdev->phys = asus->driver->input_phys;
256 asus->inputdev->id.bustype = BUS_HOST;
257 asus->inputdev->dev.parent = &asus->platform_device->dev;
258 set_bit(EV_REP, asus->inputdev->evbit);
259
260 err = sparse_keymap_setup(asus->inputdev, asus->driver->keymap, NULL);
261 if (err)
262 goto err_free_dev;
263
264 err = input_register_device(asus->inputdev);
265 if (err)
266 goto err_free_keymap;
267
268 return 0;
269
270err_free_keymap:
271 sparse_keymap_free(asus->inputdev);
272err_free_dev:
273 input_free_device(asus->inputdev);
274 return err;
275}
276
277static void asus_wmi_input_exit(struct asus_wmi *asus)
278{
279 if (asus->inputdev) {
280 sparse_keymap_free(asus->inputdev);
281 input_unregister_device(asus->inputdev);
282 }
283
284 asus->inputdev = NULL;
285}
286
287static int asus_wmi_evaluate_method(u32 method_id, u32 arg0, u32 arg1,
288 u32 *retval)
289{
290 struct bios_args args = {
291 .arg0 = arg0,
292 .arg1 = arg1,
293 };
294 struct acpi_buffer input = { (acpi_size) sizeof(args), &args };
295 struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER, NULL };
296 acpi_status status;
297 union acpi_object *obj;
298 u32 tmp = 0;
299
300 status = wmi_evaluate_method(ASUS_WMI_MGMT_GUID, 1, method_id,
301 &input, &output);
302
303 if (ACPI_FAILURE(status))
304 goto exit;
305
306 obj = (union acpi_object *)output.pointer;
307 if (obj && obj->type == ACPI_TYPE_INTEGER)
308 tmp = (u32) obj->integer.value;
309
310 if (retval)
311 *retval = tmp;
312
313 kfree(obj);
314
315exit:
316 if (ACPI_FAILURE(status))
317 return -EIO;
318
319 if (tmp == ASUS_WMI_UNSUPPORTED_METHOD)
320 return -ENODEV;
321
322 return 0;
323}
324
325static int asus_wmi_evaluate_method_agfn(const struct acpi_buffer args)
326{
327 struct acpi_buffer input;
328 u64 phys_addr;
329 u32 retval;
330 u32 status = -1;
331
332 /*
333 * Copy to dma capable address otherwise memory corruption occurs as
334 * bios has to be able to access it.
335 */
336 input.pointer = kzalloc(args.length, GFP_DMA | GFP_KERNEL);
337 input.length = args.length;
338 if (!input.pointer)
339 return -ENOMEM;
340 phys_addr = virt_to_phys(input.pointer);
341 memcpy(input.pointer, args.pointer, args.length);
342
343 status = asus_wmi_evaluate_method(ASUS_WMI_METHODID_AGFN,
344 phys_addr, 0, &retval);
345 if (!status)
346 memcpy(args.pointer, input.pointer, args.length);
347
348 kfree(input.pointer);
349 if (status)
350 return -ENXIO;
351
352 return retval;
353}
354
355static int asus_wmi_get_devstate(struct asus_wmi *asus, u32 dev_id, u32 *retval)
356{
357 return asus_wmi_evaluate_method(asus->dsts_id, dev_id, 0, retval);
358}
359
360static int asus_wmi_set_devstate(u32 dev_id, u32 ctrl_param,
361 u32 *retval)
362{
363 return asus_wmi_evaluate_method(ASUS_WMI_METHODID_DEVS, dev_id,
364 ctrl_param, retval);
365}
366
367/* Helper for special devices with magic return codes */
368static int asus_wmi_get_devstate_bits(struct asus_wmi *asus,
369 u32 dev_id, u32 mask)
370{
371 u32 retval = 0;
372 int err;
373
374 err = asus_wmi_get_devstate(asus, dev_id, &retval);
375
376 if (err < 0)
377 return err;
378
379 if (!(retval & ASUS_WMI_DSTS_PRESENCE_BIT))
380 return -ENODEV;
381
382 if (mask == ASUS_WMI_DSTS_STATUS_BIT) {
383 if (retval & ASUS_WMI_DSTS_UNKNOWN_BIT)
384 return -ENODEV;
385 }
386
387 return retval & mask;
388}
389
390static int asus_wmi_get_devstate_simple(struct asus_wmi *asus, u32 dev_id)
391{
392 return asus_wmi_get_devstate_bits(asus, dev_id,
393 ASUS_WMI_DSTS_STATUS_BIT);
394}
395
396/*
397 * LEDs
398 */
399/*
400 * These functions actually update the LED's, and are called from a
401 * workqueue. By doing this as separate work rather than when the LED
402 * subsystem asks, we avoid messing with the Asus ACPI stuff during a
403 * potentially bad time, such as a timer interrupt.
404 */
405static void tpd_led_update(struct work_struct *work)
406{
407 int ctrl_param;
408 struct asus_wmi *asus;
409
410 asus = container_of(work, struct asus_wmi, tpd_led_work);
411
412 ctrl_param = asus->tpd_led_wk;
413 asus_wmi_set_devstate(ASUS_WMI_DEVID_TOUCHPAD_LED, ctrl_param, NULL);
414}
415
416static void tpd_led_set(struct led_classdev *led_cdev,
417 enum led_brightness value)
418{
419 struct asus_wmi *asus;
420
421 asus = container_of(led_cdev, struct asus_wmi, tpd_led);
422
423 asus->tpd_led_wk = !!value;
424 queue_work(asus->led_workqueue, &asus->tpd_led_work);
425}
426
427static int read_tpd_led_state(struct asus_wmi *asus)
428{
429 return asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_TOUCHPAD_LED);
430}
431
432static enum led_brightness tpd_led_get(struct led_classdev *led_cdev)
433{
434 struct asus_wmi *asus;
435
436 asus = container_of(led_cdev, struct asus_wmi, tpd_led);
437
438 return read_tpd_led_state(asus);
439}
440
441static void kbd_led_update(struct work_struct *work)
442{
443 int ctrl_param = 0;
444 struct asus_wmi *asus;
445
446 asus = container_of(work, struct asus_wmi, kbd_led_work);
447
448 /*
449 * bits 0-2: level
450 * bit 7: light on/off
451 */
452 if (asus->kbd_led_wk > 0)
453 ctrl_param = 0x80 | (asus->kbd_led_wk & 0x7F);
454
455 asus_wmi_set_devstate(ASUS_WMI_DEVID_KBD_BACKLIGHT, ctrl_param, NULL);
456}
457
458static int kbd_led_read(struct asus_wmi *asus, int *level, int *env)
459{
460 int retval;
461
462 /*
463 * bits 0-2: level
464 * bit 7: light on/off
465 * bit 8-10: environment (0: dark, 1: normal, 2: light)
466 * bit 17: status unknown
467 */
468 retval = asus_wmi_get_devstate_bits(asus, ASUS_WMI_DEVID_KBD_BACKLIGHT,
469 0xFFFF);
470
471 /* Unknown status is considered as off */
472 if (retval == 0x8000)
473 retval = 0;
474
475 if (retval >= 0) {
476 if (level)
477 *level = retval & 0x7F;
478 if (env)
479 *env = (retval >> 8) & 0x7F;
480 retval = 0;
481 }
482
483 return retval;
484}
485
486static void kbd_led_set(struct led_classdev *led_cdev,
487 enum led_brightness value)
488{
489 struct asus_wmi *asus;
490
491 asus = container_of(led_cdev, struct asus_wmi, kbd_led);
492
493 if (value > asus->kbd_led.max_brightness)
494 value = asus->kbd_led.max_brightness;
495 else if (value < 0)
496 value = 0;
497
498 asus->kbd_led_wk = value;
499 queue_work(asus->led_workqueue, &asus->kbd_led_work);
500}
501
502static enum led_brightness kbd_led_get(struct led_classdev *led_cdev)
503{
504 struct asus_wmi *asus;
505 int retval, value;
506
507 asus = container_of(led_cdev, struct asus_wmi, kbd_led);
508
509 retval = kbd_led_read(asus, &value, NULL);
510
511 if (retval < 0)
512 return retval;
513
514 return value;
515}
516
517static int wlan_led_unknown_state(struct asus_wmi *asus)
518{
519 u32 result;
520
521 asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_WIRELESS_LED, &result);
522
523 return result & ASUS_WMI_DSTS_UNKNOWN_BIT;
524}
525
526static int wlan_led_presence(struct asus_wmi *asus)
527{
528 u32 result;
529
530 asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_WIRELESS_LED, &result);
531
532 return result & ASUS_WMI_DSTS_PRESENCE_BIT;
533}
534
535static void wlan_led_update(struct work_struct *work)
536{
537 int ctrl_param;
538 struct asus_wmi *asus;
539
540 asus = container_of(work, struct asus_wmi, wlan_led_work);
541
542 ctrl_param = asus->wlan_led_wk;
543 asus_wmi_set_devstate(ASUS_WMI_DEVID_WIRELESS_LED, ctrl_param, NULL);
544}
545
546static void wlan_led_set(struct led_classdev *led_cdev,
547 enum led_brightness value)
548{
549 struct asus_wmi *asus;
550
551 asus = container_of(led_cdev, struct asus_wmi, wlan_led);
552
553 asus->wlan_led_wk = !!value;
554 queue_work(asus->led_workqueue, &asus->wlan_led_work);
555}
556
557static enum led_brightness wlan_led_get(struct led_classdev *led_cdev)
558{
559 struct asus_wmi *asus;
560 u32 result;
561
562 asus = container_of(led_cdev, struct asus_wmi, wlan_led);
563 asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_WIRELESS_LED, &result);
564
565 return result & ASUS_WMI_DSTS_BRIGHTNESS_MASK;
566}
567
568static void asus_wmi_led_exit(struct asus_wmi *asus)
569{
570 if (!IS_ERR_OR_NULL(asus->kbd_led.dev))
571 led_classdev_unregister(&asus->kbd_led);
572 if (!IS_ERR_OR_NULL(asus->tpd_led.dev))
573 led_classdev_unregister(&asus->tpd_led);
574 if (!IS_ERR_OR_NULL(asus->wlan_led.dev))
575 led_classdev_unregister(&asus->wlan_led);
576 if (asus->led_workqueue)
577 destroy_workqueue(asus->led_workqueue);
578}
579
580static int asus_wmi_led_init(struct asus_wmi *asus)
581{
582 int rv = 0, led_val;
583
584 asus->led_workqueue = create_singlethread_workqueue("led_workqueue");
585 if (!asus->led_workqueue)
586 return -ENOMEM;
587
588 if (read_tpd_led_state(asus) >= 0) {
589 INIT_WORK(&asus->tpd_led_work, tpd_led_update);
590
591 asus->tpd_led.name = "asus::touchpad";
592 asus->tpd_led.brightness_set = tpd_led_set;
593 asus->tpd_led.brightness_get = tpd_led_get;
594 asus->tpd_led.max_brightness = 1;
595
596 rv = led_classdev_register(&asus->platform_device->dev,
597 &asus->tpd_led);
598 if (rv)
599 goto error;
600 }
601
602 led_val = kbd_led_read(asus, NULL, NULL);
603 if (led_val >= 0) {
604 INIT_WORK(&asus->kbd_led_work, kbd_led_update);
605
606 asus->kbd_led_wk = led_val;
607 asus->kbd_led.name = "asus::kbd_backlight";
608 asus->kbd_led.brightness_set = kbd_led_set;
609 asus->kbd_led.brightness_get = kbd_led_get;
610 asus->kbd_led.max_brightness = 3;
611
612 rv = led_classdev_register(&asus->platform_device->dev,
613 &asus->kbd_led);
614 if (rv)
615 goto error;
616 }
617
618 if (wlan_led_presence(asus) && (asus->driver->quirks->wapf > 0)) {
619 INIT_WORK(&asus->wlan_led_work, wlan_led_update);
620
621 asus->wlan_led.name = "asus::wlan";
622 asus->wlan_led.brightness_set = wlan_led_set;
623 if (!wlan_led_unknown_state(asus))
624 asus->wlan_led.brightness_get = wlan_led_get;
625 asus->wlan_led.flags = LED_CORE_SUSPENDRESUME;
626 asus->wlan_led.max_brightness = 1;
627 asus->wlan_led.default_trigger = "asus-wlan";
628
629 rv = led_classdev_register(&asus->platform_device->dev,
630 &asus->wlan_led);
631 }
632
633error:
634 if (rv)
635 asus_wmi_led_exit(asus);
636
637 return rv;
638}
639
640
641/*
642 * PCI hotplug (for wlan rfkill)
643 */
644static bool asus_wlan_rfkill_blocked(struct asus_wmi *asus)
645{
646 int result = asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_WLAN);
647
648 if (result < 0)
649 return false;
650 return !result;
651}
652
653static void asus_rfkill_hotplug(struct asus_wmi *asus)
654{
655 struct pci_dev *dev;
656 struct pci_bus *bus;
657 bool blocked;
658 bool absent;
659 u32 l;
660
661 mutex_lock(&asus->wmi_lock);
662 blocked = asus_wlan_rfkill_blocked(asus);
663 mutex_unlock(&asus->wmi_lock);
664
665 mutex_lock(&asus->hotplug_lock);
666 pci_lock_rescan_remove();
667
668 if (asus->wlan.rfkill)
669 rfkill_set_sw_state(asus->wlan.rfkill, blocked);
670
671 if (asus->hotplug_slot) {
672 bus = pci_find_bus(0, 1);
673 if (!bus) {
674 pr_warn("Unable to find PCI bus 1?\n");
675 goto out_unlock;
676 }
677
678 if (pci_bus_read_config_dword(bus, 0, PCI_VENDOR_ID, &l)) {
679 pr_err("Unable to read PCI config space?\n");
680 goto out_unlock;
681 }
682 absent = (l == 0xffffffff);
683
684 if (blocked != absent) {
685 pr_warn("BIOS says wireless lan is %s, "
686 "but the pci device is %s\n",
687 blocked ? "blocked" : "unblocked",
688 absent ? "absent" : "present");
689 pr_warn("skipped wireless hotplug as probably "
690 "inappropriate for this model\n");
691 goto out_unlock;
692 }
693
694 if (!blocked) {
695 dev = pci_get_slot(bus, 0);
696 if (dev) {
697 /* Device already present */
698 pci_dev_put(dev);
699 goto out_unlock;
700 }
701 dev = pci_scan_single_device(bus, 0);
702 if (dev) {
703 pci_bus_assign_resources(bus);
704 pci_bus_add_device(dev);
705 }
706 } else {
707 dev = pci_get_slot(bus, 0);
708 if (dev) {
709 pci_stop_and_remove_bus_device(dev);
710 pci_dev_put(dev);
711 }
712 }
713 }
714
715out_unlock:
716 pci_unlock_rescan_remove();
717 mutex_unlock(&asus->hotplug_lock);
718}
719
720static void asus_rfkill_notify(acpi_handle handle, u32 event, void *data)
721{
722 struct asus_wmi *asus = data;
723
724 if (event != ACPI_NOTIFY_BUS_CHECK)
725 return;
726
727 /*
728 * We can't call directly asus_rfkill_hotplug because most
729 * of the time WMBC is still being executed and not reetrant.
730 * There is currently no way to tell ACPICA that we want this
731 * method to be serialized, we schedule a asus_rfkill_hotplug
732 * call later, in a safer context.
733 */
734 queue_work(asus->hotplug_workqueue, &asus->hotplug_work);
735}
736
737static int asus_register_rfkill_notifier(struct asus_wmi *asus, char *node)
738{
739 acpi_status status;
740 acpi_handle handle;
741
742 status = acpi_get_handle(NULL, node, &handle);
743
744 if (ACPI_SUCCESS(status)) {
745 status = acpi_install_notify_handler(handle,
746 ACPI_SYSTEM_NOTIFY,
747 asus_rfkill_notify, asus);
748 if (ACPI_FAILURE(status))
749 pr_warn("Failed to register notify on %s\n", node);
750 } else
751 return -ENODEV;
752
753 return 0;
754}
755
756static void asus_unregister_rfkill_notifier(struct asus_wmi *asus, char *node)
757{
758 acpi_status status = AE_OK;
759 acpi_handle handle;
760
761 status = acpi_get_handle(NULL, node, &handle);
762
763 if (ACPI_SUCCESS(status)) {
764 status = acpi_remove_notify_handler(handle,
765 ACPI_SYSTEM_NOTIFY,
766 asus_rfkill_notify);
767 if (ACPI_FAILURE(status))
768 pr_err("Error removing rfkill notify handler %s\n",
769 node);
770 }
771}
772
773static int asus_get_adapter_status(struct hotplug_slot *hotplug_slot,
774 u8 *value)
775{
776 struct asus_wmi *asus = hotplug_slot->private;
777 int result = asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_WLAN);
778
779 if (result < 0)
780 return result;
781
782 *value = !!result;
783 return 0;
784}
785
786static void asus_cleanup_pci_hotplug(struct hotplug_slot *hotplug_slot)
787{
788 kfree(hotplug_slot->info);
789 kfree(hotplug_slot);
790}
791
792static struct hotplug_slot_ops asus_hotplug_slot_ops = {
793 .owner = THIS_MODULE,
794 .get_adapter_status = asus_get_adapter_status,
795 .get_power_status = asus_get_adapter_status,
796};
797
798static void asus_hotplug_work(struct work_struct *work)
799{
800 struct asus_wmi *asus;
801
802 asus = container_of(work, struct asus_wmi, hotplug_work);
803 asus_rfkill_hotplug(asus);
804}
805
806static int asus_setup_pci_hotplug(struct asus_wmi *asus)
807{
808 int ret = -ENOMEM;
809 struct pci_bus *bus = pci_find_bus(0, 1);
810
811 if (!bus) {
812 pr_err("Unable to find wifi PCI bus\n");
813 return -ENODEV;
814 }
815
816 asus->hotplug_workqueue =
817 create_singlethread_workqueue("hotplug_workqueue");
818 if (!asus->hotplug_workqueue)
819 goto error_workqueue;
820
821 INIT_WORK(&asus->hotplug_work, asus_hotplug_work);
822
823 asus->hotplug_slot = kzalloc(sizeof(struct hotplug_slot), GFP_KERNEL);
824 if (!asus->hotplug_slot)
825 goto error_slot;
826
827 asus->hotplug_slot->info = kzalloc(sizeof(struct hotplug_slot_info),
828 GFP_KERNEL);
829 if (!asus->hotplug_slot->info)
830 goto error_info;
831
832 asus->hotplug_slot->private = asus;
833 asus->hotplug_slot->release = &asus_cleanup_pci_hotplug;
834 asus->hotplug_slot->ops = &asus_hotplug_slot_ops;
835 asus_get_adapter_status(asus->hotplug_slot,
836 &asus->hotplug_slot->info->adapter_status);
837
838 ret = pci_hp_register(asus->hotplug_slot, bus, 0, "asus-wifi");
839 if (ret) {
840 pr_err("Unable to register hotplug slot - %d\n", ret);
841 goto error_register;
842 }
843
844 return 0;
845
846error_register:
847 kfree(asus->hotplug_slot->info);
848error_info:
849 kfree(asus->hotplug_slot);
850 asus->hotplug_slot = NULL;
851error_slot:
852 destroy_workqueue(asus->hotplug_workqueue);
853error_workqueue:
854 return ret;
855}
856
857/*
858 * Rfkill devices
859 */
860static int asus_rfkill_set(void *data, bool blocked)
861{
862 struct asus_rfkill *priv = data;
863 u32 ctrl_param = !blocked;
864 u32 dev_id = priv->dev_id;
865
866 /*
867 * If the user bit is set, BIOS can't set and record the wlan status,
868 * it will report the value read from id ASUS_WMI_DEVID_WLAN_LED
869 * while we query the wlan status through WMI(ASUS_WMI_DEVID_WLAN).
870 * So, we have to record wlan status in id ASUS_WMI_DEVID_WLAN_LED
871 * while setting the wlan status through WMI.
872 * This is also the behavior that windows app will do.
873 */
874 if ((dev_id == ASUS_WMI_DEVID_WLAN) &&
875 priv->asus->driver->wlan_ctrl_by_user)
876 dev_id = ASUS_WMI_DEVID_WLAN_LED;
877
878 return asus_wmi_set_devstate(dev_id, ctrl_param, NULL);
879}
880
881static void asus_rfkill_query(struct rfkill *rfkill, void *data)
882{
883 struct asus_rfkill *priv = data;
884 int result;
885
886 result = asus_wmi_get_devstate_simple(priv->asus, priv->dev_id);
887
888 if (result < 0)
889 return;
890
891 rfkill_set_sw_state(priv->rfkill, !result);
892}
893
894static int asus_rfkill_wlan_set(void *data, bool blocked)
895{
896 struct asus_rfkill *priv = data;
897 struct asus_wmi *asus = priv->asus;
898 int ret;
899
900 /*
901 * This handler is enabled only if hotplug is enabled.
902 * In this case, the asus_wmi_set_devstate() will
903 * trigger a wmi notification and we need to wait
904 * this call to finish before being able to call
905 * any wmi method
906 */
907 mutex_lock(&asus->wmi_lock);
908 ret = asus_rfkill_set(data, blocked);
909 mutex_unlock(&asus->wmi_lock);
910 return ret;
911}
912
913static const struct rfkill_ops asus_rfkill_wlan_ops = {
914 .set_block = asus_rfkill_wlan_set,
915 .query = asus_rfkill_query,
916};
917
918static const struct rfkill_ops asus_rfkill_ops = {
919 .set_block = asus_rfkill_set,
920 .query = asus_rfkill_query,
921};
922
923static int asus_new_rfkill(struct asus_wmi *asus,
924 struct asus_rfkill *arfkill,
925 const char *name, enum rfkill_type type, int dev_id)
926{
927 int result = asus_wmi_get_devstate_simple(asus, dev_id);
928 struct rfkill **rfkill = &arfkill->rfkill;
929
930 if (result < 0)
931 return result;
932
933 arfkill->dev_id = dev_id;
934 arfkill->asus = asus;
935
936 if (dev_id == ASUS_WMI_DEVID_WLAN &&
937 asus->driver->quirks->hotplug_wireless)
938 *rfkill = rfkill_alloc(name, &asus->platform_device->dev, type,
939 &asus_rfkill_wlan_ops, arfkill);
940 else
941 *rfkill = rfkill_alloc(name, &asus->platform_device->dev, type,
942 &asus_rfkill_ops, arfkill);
943
944 if (!*rfkill)
945 return -EINVAL;
946
947 if ((dev_id == ASUS_WMI_DEVID_WLAN) &&
948 (asus->driver->quirks->wapf > 0))
949 rfkill_set_led_trigger_name(*rfkill, "asus-wlan");
950
951 rfkill_init_sw_state(*rfkill, !result);
952 result = rfkill_register(*rfkill);
953 if (result) {
954 rfkill_destroy(*rfkill);
955 *rfkill = NULL;
956 return result;
957 }
958 return 0;
959}
960
961static void asus_wmi_rfkill_exit(struct asus_wmi *asus)
962{
963 asus_unregister_rfkill_notifier(asus, "\\_SB.PCI0.P0P5");
964 asus_unregister_rfkill_notifier(asus, "\\_SB.PCI0.P0P6");
965 asus_unregister_rfkill_notifier(asus, "\\_SB.PCI0.P0P7");
966 if (asus->wlan.rfkill) {
967 rfkill_unregister(asus->wlan.rfkill);
968 rfkill_destroy(asus->wlan.rfkill);
969 asus->wlan.rfkill = NULL;
970 }
971 /*
972 * Refresh pci hotplug in case the rfkill state was changed after
973 * asus_unregister_rfkill_notifier()
974 */
975 asus_rfkill_hotplug(asus);
976 if (asus->hotplug_slot)
977 pci_hp_deregister(asus->hotplug_slot);
978 if (asus->hotplug_workqueue)
979 destroy_workqueue(asus->hotplug_workqueue);
980
981 if (asus->bluetooth.rfkill) {
982 rfkill_unregister(asus->bluetooth.rfkill);
983 rfkill_destroy(asus->bluetooth.rfkill);
984 asus->bluetooth.rfkill = NULL;
985 }
986 if (asus->wimax.rfkill) {
987 rfkill_unregister(asus->wimax.rfkill);
988 rfkill_destroy(asus->wimax.rfkill);
989 asus->wimax.rfkill = NULL;
990 }
991 if (asus->wwan3g.rfkill) {
992 rfkill_unregister(asus->wwan3g.rfkill);
993 rfkill_destroy(asus->wwan3g.rfkill);
994 asus->wwan3g.rfkill = NULL;
995 }
996 if (asus->gps.rfkill) {
997 rfkill_unregister(asus->gps.rfkill);
998 rfkill_destroy(asus->gps.rfkill);
999 asus->gps.rfkill = NULL;
1000 }
1001 if (asus->uwb.rfkill) {
1002 rfkill_unregister(asus->uwb.rfkill);
1003 rfkill_destroy(asus->uwb.rfkill);
1004 asus->uwb.rfkill = NULL;
1005 }
1006}
1007
1008static int asus_wmi_rfkill_init(struct asus_wmi *asus)
1009{
1010 int result = 0;
1011
1012 mutex_init(&asus->hotplug_lock);
1013 mutex_init(&asus->wmi_lock);
1014
1015 result = asus_new_rfkill(asus, &asus->wlan, "asus-wlan",
1016 RFKILL_TYPE_WLAN, ASUS_WMI_DEVID_WLAN);
1017
1018 if (result && result != -ENODEV)
1019 goto exit;
1020
1021 result = asus_new_rfkill(asus, &asus->bluetooth,
1022 "asus-bluetooth", RFKILL_TYPE_BLUETOOTH,
1023 ASUS_WMI_DEVID_BLUETOOTH);
1024
1025 if (result && result != -ENODEV)
1026 goto exit;
1027
1028 result = asus_new_rfkill(asus, &asus->wimax, "asus-wimax",
1029 RFKILL_TYPE_WIMAX, ASUS_WMI_DEVID_WIMAX);
1030
1031 if (result && result != -ENODEV)
1032 goto exit;
1033
1034 result = asus_new_rfkill(asus, &asus->wwan3g, "asus-wwan3g",
1035 RFKILL_TYPE_WWAN, ASUS_WMI_DEVID_WWAN3G);
1036
1037 if (result && result != -ENODEV)
1038 goto exit;
1039
1040 result = asus_new_rfkill(asus, &asus->gps, "asus-gps",
1041 RFKILL_TYPE_GPS, ASUS_WMI_DEVID_GPS);
1042
1043 if (result && result != -ENODEV)
1044 goto exit;
1045
1046 result = asus_new_rfkill(asus, &asus->uwb, "asus-uwb",
1047 RFKILL_TYPE_UWB, ASUS_WMI_DEVID_UWB);
1048
1049 if (result && result != -ENODEV)
1050 goto exit;
1051
1052 if (!asus->driver->quirks->hotplug_wireless)
1053 goto exit;
1054
1055 result = asus_setup_pci_hotplug(asus);
1056 /*
1057 * If we get -EBUSY then something else is handling the PCI hotplug -
1058 * don't fail in this case
1059 */
1060 if (result == -EBUSY)
1061 result = 0;
1062
1063 asus_register_rfkill_notifier(asus, "\\_SB.PCI0.P0P5");
1064 asus_register_rfkill_notifier(asus, "\\_SB.PCI0.P0P6");
1065 asus_register_rfkill_notifier(asus, "\\_SB.PCI0.P0P7");
1066 /*
1067 * Refresh pci hotplug in case the rfkill state was changed during
1068 * setup.
1069 */
1070 asus_rfkill_hotplug(asus);
1071
1072exit:
1073 if (result && result != -ENODEV)
1074 asus_wmi_rfkill_exit(asus);
1075
1076 if (result == -ENODEV)
1077 result = 0;
1078
1079 return result;
1080}
1081
1082/*
1083 * Hwmon device
1084 */
1085static int asus_hwmon_agfn_fan_speed_read(struct asus_wmi *asus, int fan,
1086 int *speed)
1087{
1088 struct fan_args args = {
1089 .agfn.len = sizeof(args),
1090 .agfn.mfun = ASUS_FAN_MFUN,
1091 .agfn.sfun = ASUS_FAN_SFUN_READ,
1092 .fan = fan,
1093 .speed = 0,
1094 };
1095 struct acpi_buffer input = { (acpi_size) sizeof(args), &args };
1096 int status;
1097
1098 if (fan != 1)
1099 return -EINVAL;
1100
1101 status = asus_wmi_evaluate_method_agfn(input);
1102
1103 if (status || args.agfn.err)
1104 return -ENXIO;
1105
1106 if (speed)
1107 *speed = args.speed;
1108
1109 return 0;
1110}
1111
1112static int asus_hwmon_agfn_fan_speed_write(struct asus_wmi *asus, int fan,
1113 int *speed)
1114{
1115 struct fan_args args = {
1116 .agfn.len = sizeof(args),
1117 .agfn.mfun = ASUS_FAN_MFUN,
1118 .agfn.sfun = ASUS_FAN_SFUN_WRITE,
1119 .fan = fan,
1120 .speed = speed ? *speed : 0,
1121 };
1122 struct acpi_buffer input = { (acpi_size) sizeof(args), &args };
1123 int status;
1124
1125 /* 1: for setting 1st fan's speed 0: setting auto mode */
1126 if (fan != 1 && fan != 0)
1127 return -EINVAL;
1128
1129 status = asus_wmi_evaluate_method_agfn(input);
1130
1131 if (status || args.agfn.err)
1132 return -ENXIO;
1133
1134 if (speed && fan == 1)
1135 asus->asus_hwmon_pwm = *speed;
1136
1137 return 0;
1138}
1139
1140/*
1141 * Check if we can read the speed of one fan. If true we assume we can also
1142 * control it.
1143 */
1144static int asus_hwmon_get_fan_number(struct asus_wmi *asus, int *num_fans)
1145{
1146 int status;
1147 int speed = 0;
1148
1149 *num_fans = 0;
1150
1151 status = asus_hwmon_agfn_fan_speed_read(asus, 1, &speed);
1152 if (!status)
1153 *num_fans = 1;
1154
1155 return 0;
1156}
1157
1158static int asus_hwmon_fan_set_auto(struct asus_wmi *asus)
1159{
1160 int status;
1161
1162 status = asus_hwmon_agfn_fan_speed_write(asus, 0, NULL);
1163 if (status)
1164 return -ENXIO;
1165
1166 asus->asus_hwmon_fan_manual_mode = false;
1167
1168 return 0;
1169}
1170
1171static int asus_hwmon_fan_rpm_show(struct device *dev, int fan)
1172{
1173 struct asus_wmi *asus = dev_get_drvdata(dev);
1174 int value;
1175 int ret;
1176
1177 /* no speed readable on manual mode */
1178 if (asus->asus_hwmon_fan_manual_mode)
1179 return -ENXIO;
1180
1181 ret = asus_hwmon_agfn_fan_speed_read(asus, fan+1, &value);
1182 if (ret) {
1183 pr_warn("reading fan speed failed: %d\n", ret);
1184 return -ENXIO;
1185 }
1186
1187 return value;
1188}
1189
1190static void asus_hwmon_pwm_show(struct asus_wmi *asus, int fan, int *value)
1191{
1192 int err;
1193
1194 if (asus->asus_hwmon_pwm >= 0) {
1195 *value = asus->asus_hwmon_pwm;
1196 return;
1197 }
1198
1199 err = asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_FAN_CTRL, value);
1200 if (err < 0)
1201 return;
1202
1203 *value &= 0xFF;
1204
1205 if (*value == 1) /* Low Speed */
1206 *value = 85;
1207 else if (*value == 2)
1208 *value = 170;
1209 else if (*value == 3)
1210 *value = 255;
1211 else if (*value) {
1212 pr_err("Unknown fan speed %#x\n", *value);
1213 *value = -1;
1214 }
1215}
1216
1217static ssize_t pwm1_show(struct device *dev,
1218 struct device_attribute *attr,
1219 char *buf)
1220{
1221 struct asus_wmi *asus = dev_get_drvdata(dev);
1222 int value;
1223
1224 asus_hwmon_pwm_show(asus, 0, &value);
1225
1226 return sprintf(buf, "%d\n", value);
1227}
1228
1229static ssize_t pwm1_store(struct device *dev,
1230 struct device_attribute *attr,
1231 const char *buf, size_t count) {
1232 struct asus_wmi *asus = dev_get_drvdata(dev);
1233 int value;
1234 int state;
1235 int ret;
1236
1237 ret = kstrtouint(buf, 10, &value);
1238
1239 if (ret)
1240 return ret;
1241
1242 value = clamp(value, 0, 255);
1243
1244 state = asus_hwmon_agfn_fan_speed_write(asus, 1, &value);
1245 if (state)
1246 pr_warn("Setting fan speed failed: %d\n", state);
1247 else
1248 asus->asus_hwmon_fan_manual_mode = true;
1249
1250 return count;
1251}
1252
1253static ssize_t fan1_input_show(struct device *dev,
1254 struct device_attribute *attr,
1255 char *buf)
1256{
1257 int value = asus_hwmon_fan_rpm_show(dev, 0);
1258
1259 return sprintf(buf, "%d\n", value < 0 ? -1 : value*100);
1260
1261}
1262
1263static ssize_t pwm1_enable_show(struct device *dev,
1264 struct device_attribute *attr,
1265 char *buf)
1266{
1267 struct asus_wmi *asus = dev_get_drvdata(dev);
1268
1269 if (asus->asus_hwmon_fan_manual_mode)
1270 return sprintf(buf, "%d\n", ASUS_FAN_CTRL_MANUAL);
1271
1272 return sprintf(buf, "%d\n", ASUS_FAN_CTRL_AUTO);
1273}
1274
1275static ssize_t pwm1_enable_store(struct device *dev,
1276 struct device_attribute *attr,
1277 const char *buf, size_t count)
1278{
1279 struct asus_wmi *asus = dev_get_drvdata(dev);
1280 int status = 0;
1281 int state;
1282 int ret;
1283
1284 ret = kstrtouint(buf, 10, &state);
1285
1286 if (ret)
1287 return ret;
1288
1289 if (state == ASUS_FAN_CTRL_MANUAL)
1290 asus->asus_hwmon_fan_manual_mode = true;
1291 else
1292 status = asus_hwmon_fan_set_auto(asus);
1293
1294 if (status)
1295 return status;
1296
1297 return count;
1298}
1299
1300static ssize_t fan1_label_show(struct device *dev,
1301 struct device_attribute *attr,
1302 char *buf)
1303{
1304 return sprintf(buf, "%s\n", ASUS_FAN_DESC);
1305}
1306
1307static ssize_t asus_hwmon_temp1(struct device *dev,
1308 struct device_attribute *attr,
1309 char *buf)
1310{
1311 struct asus_wmi *asus = dev_get_drvdata(dev);
1312 u32 value;
1313 int err;
1314
1315 err = asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_THERMAL_CTRL, &value);
1316
1317 if (err < 0)
1318 return err;
1319
1320 value = DECI_KELVIN_TO_CELSIUS((value & 0xFFFF)) * 1000;
1321
1322 return sprintf(buf, "%d\n", value);
1323}
1324
1325/* Fan1 */
1326static DEVICE_ATTR_RW(pwm1);
1327static DEVICE_ATTR_RW(pwm1_enable);
1328static DEVICE_ATTR_RO(fan1_input);
1329static DEVICE_ATTR_RO(fan1_label);
1330
1331/* Temperature */
1332static DEVICE_ATTR(temp1_input, S_IRUGO, asus_hwmon_temp1, NULL);
1333
1334static struct attribute *hwmon_attributes[] = {
1335 &dev_attr_pwm1.attr,
1336 &dev_attr_pwm1_enable.attr,
1337 &dev_attr_fan1_input.attr,
1338 &dev_attr_fan1_label.attr,
1339
1340 &dev_attr_temp1_input.attr,
1341 NULL
1342};
1343
1344static umode_t asus_hwmon_sysfs_is_visible(struct kobject *kobj,
1345 struct attribute *attr, int idx)
1346{
1347 struct device *dev = container_of(kobj, struct device, kobj);
1348 struct platform_device *pdev = to_platform_device(dev->parent);
1349 struct asus_wmi *asus = platform_get_drvdata(pdev);
1350 int dev_id = -1;
1351 int fan_attr = -1;
1352 u32 value = ASUS_WMI_UNSUPPORTED_METHOD;
1353 bool ok = true;
1354
1355 if (attr == &dev_attr_pwm1.attr)
1356 dev_id = ASUS_WMI_DEVID_FAN_CTRL;
1357 else if (attr == &dev_attr_temp1_input.attr)
1358 dev_id = ASUS_WMI_DEVID_THERMAL_CTRL;
1359
1360
1361 if (attr == &dev_attr_fan1_input.attr
1362 || attr == &dev_attr_fan1_label.attr
1363 || attr == &dev_attr_pwm1.attr
1364 || attr == &dev_attr_pwm1_enable.attr) {
1365 fan_attr = 1;
1366 }
1367
1368 if (dev_id != -1) {
1369 int err = asus_wmi_get_devstate(asus, dev_id, &value);
1370
1371 if (err < 0 && fan_attr == -1)
1372 return 0; /* can't return negative here */
1373 }
1374
1375 if (dev_id == ASUS_WMI_DEVID_FAN_CTRL) {
1376 /*
1377 * We need to find a better way, probably using sfun,
1378 * bits or spec ...
1379 * Currently we disable it if:
1380 * - ASUS_WMI_UNSUPPORTED_METHOD is returned
1381 * - reverved bits are non-zero
1382 * - sfun and presence bit are not set
1383 */
1384 if (value == ASUS_WMI_UNSUPPORTED_METHOD || value & 0xFFF80000
1385 || (!asus->sfun && !(value & ASUS_WMI_DSTS_PRESENCE_BIT)))
1386 ok = false;
1387 else
1388 ok = fan_attr <= asus->asus_hwmon_num_fans;
1389 } else if (dev_id == ASUS_WMI_DEVID_THERMAL_CTRL) {
1390 /* If value is zero, something is clearly wrong */
1391 if (!value)
1392 ok = false;
1393 } else if (fan_attr <= asus->asus_hwmon_num_fans && fan_attr != -1) {
1394 ok = true;
1395 } else {
1396 ok = false;
1397 }
1398
1399 return ok ? attr->mode : 0;
1400}
1401
1402static struct attribute_group hwmon_attribute_group = {
1403 .is_visible = asus_hwmon_sysfs_is_visible,
1404 .attrs = hwmon_attributes
1405};
1406__ATTRIBUTE_GROUPS(hwmon_attribute);
1407
1408static int asus_wmi_hwmon_init(struct asus_wmi *asus)
1409{
1410 struct device *hwmon;
1411
1412 hwmon = hwmon_device_register_with_groups(&asus->platform_device->dev,
1413 "asus", asus,
1414 hwmon_attribute_groups);
1415 if (IS_ERR(hwmon)) {
1416 pr_err("Could not register asus hwmon device\n");
1417 return PTR_ERR(hwmon);
1418 }
1419 return 0;
1420}
1421
1422/*
1423 * Backlight
1424 */
1425static int read_backlight_power(struct asus_wmi *asus)
1426{
1427 int ret;
1428 if (asus->driver->quirks->store_backlight_power)
1429 ret = !asus->driver->panel_power;
1430 else
1431 ret = asus_wmi_get_devstate_simple(asus,
1432 ASUS_WMI_DEVID_BACKLIGHT);
1433
1434 if (ret < 0)
1435 return ret;
1436
1437 return ret ? FB_BLANK_UNBLANK : FB_BLANK_POWERDOWN;
1438}
1439
1440static int read_brightness_max(struct asus_wmi *asus)
1441{
1442 u32 retval;
1443 int err;
1444
1445 err = asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_BRIGHTNESS, &retval);
1446
1447 if (err < 0)
1448 return err;
1449
1450 retval = retval & ASUS_WMI_DSTS_MAX_BRIGTH_MASK;
1451 retval >>= 8;
1452
1453 if (!retval)
1454 return -ENODEV;
1455
1456 return retval;
1457}
1458
1459static int read_brightness(struct backlight_device *bd)
1460{
1461 struct asus_wmi *asus = bl_get_data(bd);
1462 u32 retval;
1463 int err;
1464
1465 err = asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_BRIGHTNESS, &retval);
1466
1467 if (err < 0)
1468 return err;
1469
1470 return retval & ASUS_WMI_DSTS_BRIGHTNESS_MASK;
1471}
1472
1473static u32 get_scalar_command(struct backlight_device *bd)
1474{
1475 struct asus_wmi *asus = bl_get_data(bd);
1476 u32 ctrl_param = 0;
1477
1478 if ((asus->driver->brightness < bd->props.brightness) ||
1479 bd->props.brightness == bd->props.max_brightness)
1480 ctrl_param = 0x00008001;
1481 else if ((asus->driver->brightness > bd->props.brightness) ||
1482 bd->props.brightness == 0)
1483 ctrl_param = 0x00008000;
1484
1485 asus->driver->brightness = bd->props.brightness;
1486
1487 return ctrl_param;
1488}
1489
1490static int update_bl_status(struct backlight_device *bd)
1491{
1492 struct asus_wmi *asus = bl_get_data(bd);
1493 u32 ctrl_param;
1494 int power, err = 0;
1495
1496 power = read_backlight_power(asus);
1497 if (power != -ENODEV && bd->props.power != power) {
1498 ctrl_param = !!(bd->props.power == FB_BLANK_UNBLANK);
1499 err = asus_wmi_set_devstate(ASUS_WMI_DEVID_BACKLIGHT,
1500 ctrl_param, NULL);
1501 if (asus->driver->quirks->store_backlight_power)
1502 asus->driver->panel_power = bd->props.power;
1503
1504 /* When using scalar brightness, updating the brightness
1505 * will mess with the backlight power */
1506 if (asus->driver->quirks->scalar_panel_brightness)
1507 return err;
1508 }
1509
1510 if (asus->driver->quirks->scalar_panel_brightness)
1511 ctrl_param = get_scalar_command(bd);
1512 else
1513 ctrl_param = bd->props.brightness;
1514
1515 err = asus_wmi_set_devstate(ASUS_WMI_DEVID_BRIGHTNESS,
1516 ctrl_param, NULL);
1517
1518 return err;
1519}
1520
1521static const struct backlight_ops asus_wmi_bl_ops = {
1522 .get_brightness = read_brightness,
1523 .update_status = update_bl_status,
1524};
1525
1526static int asus_wmi_backlight_notify(struct asus_wmi *asus, int code)
1527{
1528 struct backlight_device *bd = asus->backlight_device;
1529 int old = bd->props.brightness;
1530 int new = old;
1531
1532 if (code >= NOTIFY_BRNUP_MIN && code <= NOTIFY_BRNUP_MAX)
1533 new = code - NOTIFY_BRNUP_MIN + 1;
1534 else if (code >= NOTIFY_BRNDOWN_MIN && code <= NOTIFY_BRNDOWN_MAX)
1535 new = code - NOTIFY_BRNDOWN_MIN;
1536
1537 bd->props.brightness = new;
1538 backlight_update_status(bd);
1539 backlight_force_update(bd, BACKLIGHT_UPDATE_HOTKEY);
1540
1541 return old;
1542}
1543
1544static int asus_wmi_backlight_init(struct asus_wmi *asus)
1545{
1546 struct backlight_device *bd;
1547 struct backlight_properties props;
1548 int max;
1549 int power;
1550
1551 max = read_brightness_max(asus);
1552 if (max < 0)
1553 return max;
1554
1555 power = read_backlight_power(asus);
1556
1557 if (power == -ENODEV)
1558 power = FB_BLANK_UNBLANK;
1559 else if (power < 0)
1560 return power;
1561
1562 memset(&props, 0, sizeof(struct backlight_properties));
1563 props.type = BACKLIGHT_PLATFORM;
1564 props.max_brightness = max;
1565 bd = backlight_device_register(asus->driver->name,
1566 &asus->platform_device->dev, asus,
1567 &asus_wmi_bl_ops, &props);
1568 if (IS_ERR(bd)) {
1569 pr_err("Could not register backlight device\n");
1570 return PTR_ERR(bd);
1571 }
1572
1573 asus->backlight_device = bd;
1574
1575 if (asus->driver->quirks->store_backlight_power)
1576 asus->driver->panel_power = power;
1577
1578 bd->props.brightness = read_brightness(bd);
1579 bd->props.power = power;
1580 backlight_update_status(bd);
1581
1582 asus->driver->brightness = bd->props.brightness;
1583
1584 return 0;
1585}
1586
1587static void asus_wmi_backlight_exit(struct asus_wmi *asus)
1588{
1589 backlight_device_unregister(asus->backlight_device);
1590
1591 asus->backlight_device = NULL;
1592}
1593
1594static int is_display_toggle(int code)
1595{
1596 /* display toggle keys */
1597 if ((code >= 0x61 && code <= 0x67) ||
1598 (code >= 0x8c && code <= 0x93) ||
1599 (code >= 0xa0 && code <= 0xa7) ||
1600 (code >= 0xd0 && code <= 0xd5))
1601 return 1;
1602
1603 return 0;
1604}
1605
1606static void asus_wmi_notify(u32 value, void *context)
1607{
1608 struct asus_wmi *asus = context;
1609 struct acpi_buffer response = { ACPI_ALLOCATE_BUFFER, NULL };
1610 union acpi_object *obj;
1611 acpi_status status;
1612 int code;
1613 int orig_code;
1614 unsigned int key_value = 1;
1615 bool autorelease = 1;
1616
1617 status = wmi_get_event_data(value, &response);
1618 if (status != AE_OK) {
1619 pr_err("bad event status 0x%x\n", status);
1620 return;
1621 }
1622
1623 obj = (union acpi_object *)response.pointer;
1624
1625 if (!obj || obj->type != ACPI_TYPE_INTEGER)
1626 goto exit;
1627
1628 code = obj->integer.value;
1629 orig_code = code;
1630
1631 if (asus->driver->key_filter) {
1632 asus->driver->key_filter(asus->driver, &code, &key_value,
1633 &autorelease);
1634 if (code == ASUS_WMI_KEY_IGNORE)
1635 goto exit;
1636 }
1637
1638 if (code >= NOTIFY_BRNUP_MIN && code <= NOTIFY_BRNUP_MAX)
1639 code = ASUS_WMI_BRN_UP;
1640 else if (code >= NOTIFY_BRNDOWN_MIN &&
1641 code <= NOTIFY_BRNDOWN_MAX)
1642 code = ASUS_WMI_BRN_DOWN;
1643
1644 if (code == ASUS_WMI_BRN_DOWN || code == ASUS_WMI_BRN_UP) {
1645 if (acpi_video_get_backlight_type() == acpi_backlight_vendor) {
1646 asus_wmi_backlight_notify(asus, orig_code);
1647 goto exit;
1648 }
1649 }
1650
1651 if (is_display_toggle(code) &&
1652 asus->driver->quirks->no_display_toggle)
1653 goto exit;
1654
1655 if (!sparse_keymap_report_event(asus->inputdev, code,
1656 key_value, autorelease))
1657 pr_info("Unknown key %x pressed\n", code);
1658
1659exit:
1660 kfree(obj);
1661}
1662
1663/*
1664 * Sys helpers
1665 */
1666static int parse_arg(const char *buf, unsigned long count, int *val)
1667{
1668 if (!count)
1669 return 0;
1670 if (sscanf(buf, "%i", val) != 1)
1671 return -EINVAL;
1672 return count;
1673}
1674
1675static ssize_t store_sys_wmi(struct asus_wmi *asus, int devid,
1676 const char *buf, size_t count)
1677{
1678 u32 retval;
1679 int rv, err, value;
1680
1681 value = asus_wmi_get_devstate_simple(asus, devid);
1682 if (value < 0)
1683 return value;
1684
1685 rv = parse_arg(buf, count, &value);
1686 err = asus_wmi_set_devstate(devid, value, &retval);
1687
1688 if (err < 0)
1689 return err;
1690
1691 return rv;
1692}
1693
1694static ssize_t show_sys_wmi(struct asus_wmi *asus, int devid, char *buf)
1695{
1696 int value = asus_wmi_get_devstate_simple(asus, devid);
1697
1698 if (value < 0)
1699 return value;
1700
1701 return sprintf(buf, "%d\n", value);
1702}
1703
1704#define ASUS_WMI_CREATE_DEVICE_ATTR(_name, _mode, _cm) \
1705 static ssize_t show_##_name(struct device *dev, \
1706 struct device_attribute *attr, \
1707 char *buf) \
1708 { \
1709 struct asus_wmi *asus = dev_get_drvdata(dev); \
1710 \
1711 return show_sys_wmi(asus, _cm, buf); \
1712 } \
1713 static ssize_t store_##_name(struct device *dev, \
1714 struct device_attribute *attr, \
1715 const char *buf, size_t count) \
1716 { \
1717 struct asus_wmi *asus = dev_get_drvdata(dev); \
1718 \
1719 return store_sys_wmi(asus, _cm, buf, count); \
1720 } \
1721 static struct device_attribute dev_attr_##_name = { \
1722 .attr = { \
1723 .name = __stringify(_name), \
1724 .mode = _mode }, \
1725 .show = show_##_name, \
1726 .store = store_##_name, \
1727 }
1728
1729ASUS_WMI_CREATE_DEVICE_ATTR(touchpad, 0644, ASUS_WMI_DEVID_TOUCHPAD);
1730ASUS_WMI_CREATE_DEVICE_ATTR(camera, 0644, ASUS_WMI_DEVID_CAMERA);
1731ASUS_WMI_CREATE_DEVICE_ATTR(cardr, 0644, ASUS_WMI_DEVID_CARDREADER);
1732ASUS_WMI_CREATE_DEVICE_ATTR(lid_resume, 0644, ASUS_WMI_DEVID_LID_RESUME);
1733
1734static ssize_t store_cpufv(struct device *dev, struct device_attribute *attr,
1735 const char *buf, size_t count)
1736{
1737 int value, rv;
1738
1739 if (!count || sscanf(buf, "%i", &value) != 1)
1740 return -EINVAL;
1741 if (value < 0 || value > 2)
1742 return -EINVAL;
1743
1744 rv = asus_wmi_evaluate_method(ASUS_WMI_METHODID_CFVS, value, 0, NULL);
1745 if (rv < 0)
1746 return rv;
1747
1748 return count;
1749}
1750
1751static DEVICE_ATTR(cpufv, S_IRUGO | S_IWUSR, NULL, store_cpufv);
1752
1753static struct attribute *platform_attributes[] = {
1754 &dev_attr_cpufv.attr,
1755 &dev_attr_camera.attr,
1756 &dev_attr_cardr.attr,
1757 &dev_attr_touchpad.attr,
1758 &dev_attr_lid_resume.attr,
1759 NULL
1760};
1761
1762static umode_t asus_sysfs_is_visible(struct kobject *kobj,
1763 struct attribute *attr, int idx)
1764{
1765 struct device *dev = container_of(kobj, struct device, kobj);
1766 struct platform_device *pdev = to_platform_device(dev);
1767 struct asus_wmi *asus = platform_get_drvdata(pdev);
1768 bool ok = true;
1769 int devid = -1;
1770
1771 if (attr == &dev_attr_camera.attr)
1772 devid = ASUS_WMI_DEVID_CAMERA;
1773 else if (attr == &dev_attr_cardr.attr)
1774 devid = ASUS_WMI_DEVID_CARDREADER;
1775 else if (attr == &dev_attr_touchpad.attr)
1776 devid = ASUS_WMI_DEVID_TOUCHPAD;
1777 else if (attr == &dev_attr_lid_resume.attr)
1778 devid = ASUS_WMI_DEVID_LID_RESUME;
1779
1780 if (devid != -1)
1781 ok = !(asus_wmi_get_devstate_simple(asus, devid) < 0);
1782
1783 return ok ? attr->mode : 0;
1784}
1785
1786static struct attribute_group platform_attribute_group = {
1787 .is_visible = asus_sysfs_is_visible,
1788 .attrs = platform_attributes
1789};
1790
1791static void asus_wmi_sysfs_exit(struct platform_device *device)
1792{
1793 sysfs_remove_group(&device->dev.kobj, &platform_attribute_group);
1794}
1795
1796static int asus_wmi_sysfs_init(struct platform_device *device)
1797{
1798 return sysfs_create_group(&device->dev.kobj, &platform_attribute_group);
1799}
1800
1801/*
1802 * Platform device
1803 */
1804static int asus_wmi_platform_init(struct asus_wmi *asus)
1805{
1806 int rv;
1807
1808 /* INIT enable hotkeys on some models */
1809 if (!asus_wmi_evaluate_method(ASUS_WMI_METHODID_INIT, 0, 0, &rv))
1810 pr_info("Initialization: %#x\n", rv);
1811
1812 /* We don't know yet what to do with this version... */
1813 if (!asus_wmi_evaluate_method(ASUS_WMI_METHODID_SPEC, 0, 0x9, &rv)) {
1814 pr_info("BIOS WMI version: %d.%d\n", rv >> 16, rv & 0xFF);
1815 asus->spec = rv;
1816 }
1817
1818 /*
1819 * The SFUN method probably allows the original driver to get the list
1820 * of features supported by a given model. For now, 0x0100 or 0x0800
1821 * bit signifies that the laptop is equipped with a Wi-Fi MiniPCI card.
1822 * The significance of others is yet to be found.
1823 */
1824 if (!asus_wmi_evaluate_method(ASUS_WMI_METHODID_SFUN, 0, 0, &rv)) {
1825 pr_info("SFUN value: %#x\n", rv);
1826 asus->sfun = rv;
1827 }
1828
1829 /*
1830 * Eee PC and Notebooks seems to have different method_id for DSTS,
1831 * but it may also be related to the BIOS's SPEC.
1832 * Note, on most Eeepc, there is no way to check if a method exist
1833 * or note, while on notebooks, they returns 0xFFFFFFFE on failure,
1834 * but once again, SPEC may probably be used for that kind of things.
1835 */
1836 if (!asus_wmi_evaluate_method(ASUS_WMI_METHODID_DSTS, 0, 0, NULL))
1837 asus->dsts_id = ASUS_WMI_METHODID_DSTS;
1838 else
1839 asus->dsts_id = ASUS_WMI_METHODID_DSTS2;
1840
1841 /* CWAP allow to define the behavior of the Fn+F2 key,
1842 * this method doesn't seems to be present on Eee PCs */
1843 if (asus->driver->quirks->wapf >= 0)
1844 asus_wmi_set_devstate(ASUS_WMI_DEVID_CWAP,
1845 asus->driver->quirks->wapf, NULL);
1846
1847 return asus_wmi_sysfs_init(asus->platform_device);
1848}
1849
1850static void asus_wmi_platform_exit(struct asus_wmi *asus)
1851{
1852 asus_wmi_sysfs_exit(asus->platform_device);
1853}
1854
1855/*
1856 * debugfs
1857 */
1858struct asus_wmi_debugfs_node {
1859 struct asus_wmi *asus;
1860 char *name;
1861 int (*show) (struct seq_file *m, void *data);
1862};
1863
1864static int show_dsts(struct seq_file *m, void *data)
1865{
1866 struct asus_wmi *asus = m->private;
1867 int err;
1868 u32 retval = -1;
1869
1870 err = asus_wmi_get_devstate(asus, asus->debug.dev_id, &retval);
1871
1872 if (err < 0)
1873 return err;
1874
1875 seq_printf(m, "DSTS(%#x) = %#x\n", asus->debug.dev_id, retval);
1876
1877 return 0;
1878}
1879
1880static int show_devs(struct seq_file *m, void *data)
1881{
1882 struct asus_wmi *asus = m->private;
1883 int err;
1884 u32 retval = -1;
1885
1886 err = asus_wmi_set_devstate(asus->debug.dev_id, asus->debug.ctrl_param,
1887 &retval);
1888
1889 if (err < 0)
1890 return err;
1891
1892 seq_printf(m, "DEVS(%#x, %#x) = %#x\n", asus->debug.dev_id,
1893 asus->debug.ctrl_param, retval);
1894
1895 return 0;
1896}
1897
1898static int show_call(struct seq_file *m, void *data)
1899{
1900 struct asus_wmi *asus = m->private;
1901 struct bios_args args = {
1902 .arg0 = asus->debug.dev_id,
1903 .arg1 = asus->debug.ctrl_param,
1904 };
1905 struct acpi_buffer input = { (acpi_size) sizeof(args), &args };
1906 struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER, NULL };
1907 union acpi_object *obj;
1908 acpi_status status;
1909
1910 status = wmi_evaluate_method(ASUS_WMI_MGMT_GUID,
1911 1, asus->debug.method_id,
1912 &input, &output);
1913
1914 if (ACPI_FAILURE(status))
1915 return -EIO;
1916
1917 obj = (union acpi_object *)output.pointer;
1918 if (obj && obj->type == ACPI_TYPE_INTEGER)
1919 seq_printf(m, "%#x(%#x, %#x) = %#x\n", asus->debug.method_id,
1920 asus->debug.dev_id, asus->debug.ctrl_param,
1921 (u32) obj->integer.value);
1922 else
1923 seq_printf(m, "%#x(%#x, %#x) = t:%d\n", asus->debug.method_id,
1924 asus->debug.dev_id, asus->debug.ctrl_param,
1925 obj ? obj->type : -1);
1926
1927 kfree(obj);
1928
1929 return 0;
1930}
1931
1932static struct asus_wmi_debugfs_node asus_wmi_debug_files[] = {
1933 {NULL, "devs", show_devs},
1934 {NULL, "dsts", show_dsts},
1935 {NULL, "call", show_call},
1936};
1937
1938static int asus_wmi_debugfs_open(struct inode *inode, struct file *file)
1939{
1940 struct asus_wmi_debugfs_node *node = inode->i_private;
1941
1942 return single_open(file, node->show, node->asus);
1943}
1944
1945static const struct file_operations asus_wmi_debugfs_io_ops = {
1946 .owner = THIS_MODULE,
1947 .open = asus_wmi_debugfs_open,
1948 .read = seq_read,
1949 .llseek = seq_lseek,
1950 .release = single_release,
1951};
1952
1953static void asus_wmi_debugfs_exit(struct asus_wmi *asus)
1954{
1955 debugfs_remove_recursive(asus->debug.root);
1956}
1957
1958static int asus_wmi_debugfs_init(struct asus_wmi *asus)
1959{
1960 struct dentry *dent;
1961 int i;
1962
1963 asus->debug.root = debugfs_create_dir(asus->driver->name, NULL);
1964 if (!asus->debug.root) {
1965 pr_err("failed to create debugfs directory\n");
1966 goto error_debugfs;
1967 }
1968
1969 dent = debugfs_create_x32("method_id", S_IRUGO | S_IWUSR,
1970 asus->debug.root, &asus->debug.method_id);
1971 if (!dent)
1972 goto error_debugfs;
1973
1974 dent = debugfs_create_x32("dev_id", S_IRUGO | S_IWUSR,
1975 asus->debug.root, &asus->debug.dev_id);
1976 if (!dent)
1977 goto error_debugfs;
1978
1979 dent = debugfs_create_x32("ctrl_param", S_IRUGO | S_IWUSR,
1980 asus->debug.root, &asus->debug.ctrl_param);
1981 if (!dent)
1982 goto error_debugfs;
1983
1984 for (i = 0; i < ARRAY_SIZE(asus_wmi_debug_files); i++) {
1985 struct asus_wmi_debugfs_node *node = &asus_wmi_debug_files[i];
1986
1987 node->asus = asus;
1988 dent = debugfs_create_file(node->name, S_IFREG | S_IRUGO,
1989 asus->debug.root, node,
1990 &asus_wmi_debugfs_io_ops);
1991 if (!dent) {
1992 pr_err("failed to create debug file: %s\n", node->name);
1993 goto error_debugfs;
1994 }
1995 }
1996
1997 return 0;
1998
1999error_debugfs:
2000 asus_wmi_debugfs_exit(asus);
2001 return -ENOMEM;
2002}
2003
2004static int asus_wmi_fan_init(struct asus_wmi *asus)
2005{
2006 int status;
2007
2008 asus->asus_hwmon_pwm = -1;
2009 asus->asus_hwmon_num_fans = -1;
2010 asus->asus_hwmon_fan_manual_mode = false;
2011
2012 status = asus_hwmon_get_fan_number(asus, &asus->asus_hwmon_num_fans);
2013 if (status) {
2014 asus->asus_hwmon_num_fans = 0;
2015 pr_warn("Could not determine number of fans: %d\n", status);
2016 return -ENXIO;
2017 }
2018
2019 pr_info("Number of fans: %d\n", asus->asus_hwmon_num_fans);
2020 return 0;
2021}
2022
2023/*
2024 * WMI Driver
2025 */
2026static int asus_wmi_add(struct platform_device *pdev)
2027{
2028 struct platform_driver *pdrv = to_platform_driver(pdev->dev.driver);
2029 struct asus_wmi_driver *wdrv = to_asus_wmi_driver(pdrv);
2030 struct asus_wmi *asus;
2031 const char *chassis_type;
2032 acpi_status status;
2033 int err;
2034 u32 result;
2035
2036 asus = kzalloc(sizeof(struct asus_wmi), GFP_KERNEL);
2037 if (!asus)
2038 return -ENOMEM;
2039
2040 asus->driver = wdrv;
2041 asus->platform_device = pdev;
2042 wdrv->platform_device = pdev;
2043 platform_set_drvdata(asus->platform_device, asus);
2044
2045 if (wdrv->detect_quirks)
2046 wdrv->detect_quirks(asus->driver);
2047
2048 err = asus_wmi_platform_init(asus);
2049 if (err)
2050 goto fail_platform;
2051
2052 err = asus_wmi_input_init(asus);
2053 if (err)
2054 goto fail_input;
2055
2056 err = asus_wmi_fan_init(asus); /* probably no problems on error */
2057 asus_hwmon_fan_set_auto(asus);
2058
2059 err = asus_wmi_hwmon_init(asus);
2060 if (err)
2061 goto fail_hwmon;
2062
2063 err = asus_wmi_led_init(asus);
2064 if (err)
2065 goto fail_leds;
2066
2067 err = asus_wmi_rfkill_init(asus);
2068 if (err)
2069 goto fail_rfkill;
2070
2071 /* Some Asus desktop boards export an acpi-video backlight interface,
2072 stop this from showing up */
2073 chassis_type = dmi_get_system_info(DMI_CHASSIS_TYPE);
2074 if (chassis_type && !strcmp(chassis_type, "3"))
2075 acpi_video_set_dmi_backlight_type(acpi_backlight_vendor);
2076
2077 if (asus->driver->quirks->wmi_backlight_power)
2078 acpi_video_set_dmi_backlight_type(acpi_backlight_vendor);
2079
2080 if (acpi_video_get_backlight_type() == acpi_backlight_vendor) {
2081 err = asus_wmi_backlight_init(asus);
2082 if (err && err != -ENODEV)
2083 goto fail_backlight;
2084 }
2085
2086 status = wmi_install_notify_handler(asus->driver->event_guid,
2087 asus_wmi_notify, asus);
2088 if (ACPI_FAILURE(status)) {
2089 pr_err("Unable to register notify handler - %d\n", status);
2090 err = -ENODEV;
2091 goto fail_wmi_handler;
2092 }
2093
2094 err = asus_wmi_debugfs_init(asus);
2095 if (err)
2096 goto fail_debugfs;
2097
2098 asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_WLAN, &result);
2099 if (result & (ASUS_WMI_DSTS_PRESENCE_BIT | ASUS_WMI_DSTS_USER_BIT))
2100 asus->driver->wlan_ctrl_by_user = 1;
2101
2102 return 0;
2103
2104fail_debugfs:
2105 wmi_remove_notify_handler(asus->driver->event_guid);
2106fail_wmi_handler:
2107 asus_wmi_backlight_exit(asus);
2108fail_backlight:
2109 asus_wmi_rfkill_exit(asus);
2110fail_rfkill:
2111 asus_wmi_led_exit(asus);
2112fail_leds:
2113fail_hwmon:
2114 asus_wmi_input_exit(asus);
2115fail_input:
2116 asus_wmi_platform_exit(asus);
2117fail_platform:
2118 kfree(asus);
2119 return err;
2120}
2121
2122static int asus_wmi_remove(struct platform_device *device)
2123{
2124 struct asus_wmi *asus;
2125
2126 asus = platform_get_drvdata(device);
2127 wmi_remove_notify_handler(asus->driver->event_guid);
2128 asus_wmi_backlight_exit(asus);
2129 asus_wmi_input_exit(asus);
2130 asus_wmi_led_exit(asus);
2131 asus_wmi_rfkill_exit(asus);
2132 asus_wmi_debugfs_exit(asus);
2133 asus_wmi_platform_exit(asus);
2134 asus_hwmon_fan_set_auto(asus);
2135
2136 kfree(asus);
2137 return 0;
2138}
2139
2140/*
2141 * Platform driver - hibernate/resume callbacks
2142 */
2143static int asus_hotk_thaw(struct device *device)
2144{
2145 struct asus_wmi *asus = dev_get_drvdata(device);
2146
2147 if (asus->wlan.rfkill) {
2148 bool wlan;
2149
2150 /*
2151 * Work around bios bug - acpi _PTS turns off the wireless led
2152 * during suspend. Normally it restores it on resume, but
2153 * we should kick it ourselves in case hibernation is aborted.
2154 */
2155 wlan = asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_WLAN);
2156 asus_wmi_set_devstate(ASUS_WMI_DEVID_WLAN, wlan, NULL);
2157 }
2158
2159 return 0;
2160}
2161
2162static int asus_hotk_resume(struct device *device)
2163{
2164 struct asus_wmi *asus = dev_get_drvdata(device);
2165
2166 if (!IS_ERR_OR_NULL(asus->kbd_led.dev))
2167 queue_work(asus->led_workqueue, &asus->kbd_led_work);
2168
2169 return 0;
2170}
2171
2172static int asus_hotk_restore(struct device *device)
2173{
2174 struct asus_wmi *asus = dev_get_drvdata(device);
2175 int bl;
2176
2177 /* Refresh both wlan rfkill state and pci hotplug */
2178 if (asus->wlan.rfkill)
2179 asus_rfkill_hotplug(asus);
2180
2181 if (asus->bluetooth.rfkill) {
2182 bl = !asus_wmi_get_devstate_simple(asus,
2183 ASUS_WMI_DEVID_BLUETOOTH);
2184 rfkill_set_sw_state(asus->bluetooth.rfkill, bl);
2185 }
2186 if (asus->wimax.rfkill) {
2187 bl = !asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_WIMAX);
2188 rfkill_set_sw_state(asus->wimax.rfkill, bl);
2189 }
2190 if (asus->wwan3g.rfkill) {
2191 bl = !asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_WWAN3G);
2192 rfkill_set_sw_state(asus->wwan3g.rfkill, bl);
2193 }
2194 if (asus->gps.rfkill) {
2195 bl = !asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_GPS);
2196 rfkill_set_sw_state(asus->gps.rfkill, bl);
2197 }
2198 if (asus->uwb.rfkill) {
2199 bl = !asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_UWB);
2200 rfkill_set_sw_state(asus->uwb.rfkill, bl);
2201 }
2202 if (!IS_ERR_OR_NULL(asus->kbd_led.dev))
2203 queue_work(asus->led_workqueue, &asus->kbd_led_work);
2204
2205 return 0;
2206}
2207
2208static const struct dev_pm_ops asus_pm_ops = {
2209 .thaw = asus_hotk_thaw,
2210 .restore = asus_hotk_restore,
2211 .resume = asus_hotk_resume,
2212};
2213
2214static int asus_wmi_probe(struct platform_device *pdev)
2215{
2216 struct platform_driver *pdrv = to_platform_driver(pdev->dev.driver);
2217 struct asus_wmi_driver *wdrv = to_asus_wmi_driver(pdrv);
2218 int ret;
2219
2220 if (!wmi_has_guid(ASUS_WMI_MGMT_GUID)) {
2221 pr_warn("Management GUID not found\n");
2222 return -ENODEV;
2223 }
2224
2225 if (wdrv->event_guid && !wmi_has_guid(wdrv->event_guid)) {
2226 pr_warn("Event GUID not found\n");
2227 return -ENODEV;
2228 }
2229
2230 if (wdrv->probe) {
2231 ret = wdrv->probe(pdev);
2232 if (ret)
2233 return ret;
2234 }
2235
2236 return asus_wmi_add(pdev);
2237}
2238
2239static bool used;
2240
2241int __init_or_module asus_wmi_register_driver(struct asus_wmi_driver *driver)
2242{
2243 struct platform_driver *platform_driver;
2244 struct platform_device *platform_device;
2245
2246 if (used)
2247 return -EBUSY;
2248
2249 platform_driver = &driver->platform_driver;
2250 platform_driver->remove = asus_wmi_remove;
2251 platform_driver->driver.owner = driver->owner;
2252 platform_driver->driver.name = driver->name;
2253 platform_driver->driver.pm = &asus_pm_ops;
2254
2255 platform_device = platform_create_bundle(platform_driver,
2256 asus_wmi_probe,
2257 NULL, 0, NULL, 0);
2258 if (IS_ERR(platform_device))
2259 return PTR_ERR(platform_device);
2260
2261 used = true;
2262 return 0;
2263}
2264EXPORT_SYMBOL_GPL(asus_wmi_register_driver);
2265
2266void asus_wmi_unregister_driver(struct asus_wmi_driver *driver)
2267{
2268 platform_device_unregister(driver->platform_device);
2269 platform_driver_unregister(&driver->platform_driver);
2270 used = false;
2271}
2272EXPORT_SYMBOL_GPL(asus_wmi_unregister_driver);
2273
2274static int __init asus_wmi_init(void)
2275{
2276 if (!wmi_has_guid(ASUS_WMI_MGMT_GUID)) {
2277 pr_info("Asus Management GUID not found\n");
2278 return -ENODEV;
2279 }
2280
2281 pr_info("ASUS WMI generic driver loaded\n");
2282 return 0;
2283}
2284
2285static void __exit asus_wmi_exit(void)
2286{
2287 pr_info("ASUS WMI generic driver unloaded\n");
2288}
2289
2290module_init(asus_wmi_init);
2291module_exit(asus_wmi_exit);
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/acpi.h>
17#include <linux/backlight.h>
18#include <linux/debugfs.h>
19#include <linux/delay.h>
20#include <linux/dmi.h>
21#include <linux/fb.h>
22#include <linux/hwmon.h>
23#include <linux/hwmon-sysfs.h>
24#include <linux/init.h>
25#include <linux/input.h>
26#include <linux/input/sparse-keymap.h>
27#include <linux/kernel.h>
28#include <linux/leds.h>
29#include <linux/minmax.h>
30#include <linux/module.h>
31#include <linux/pci.h>
32#include <linux/pci_hotplug.h>
33#include <linux/platform_data/x86/asus-wmi.h>
34#include <linux/platform_device.h>
35#include <linux/platform_profile.h>
36#include <linux/power_supply.h>
37#include <linux/rfkill.h>
38#include <linux/seq_file.h>
39#include <linux/slab.h>
40#include <linux/types.h>
41#include <linux/units.h>
42
43#include <acpi/battery.h>
44#include <acpi/video.h>
45
46#include "asus-wmi.h"
47
48MODULE_AUTHOR("Corentin Chary <corentin.chary@gmail.com>");
49MODULE_AUTHOR("Yong Wang <yong.y.wang@intel.com>");
50MODULE_DESCRIPTION("Asus Generic WMI Driver");
51MODULE_LICENSE("GPL");
52
53static bool fnlock_default = true;
54module_param(fnlock_default, bool, 0444);
55
56#define to_asus_wmi_driver(pdrv) \
57 (container_of((pdrv), struct asus_wmi_driver, platform_driver))
58
59#define ASUS_WMI_MGMT_GUID "97845ED0-4E6D-11DE-8A39-0800200C9A66"
60
61#define NOTIFY_BRNUP_MIN 0x11
62#define NOTIFY_BRNUP_MAX 0x1f
63#define NOTIFY_BRNDOWN_MIN 0x20
64#define NOTIFY_BRNDOWN_MAX 0x2e
65#define NOTIFY_FNLOCK_TOGGLE 0x4e
66#define NOTIFY_KBD_DOCK_CHANGE 0x75
67#define NOTIFY_KBD_BRTUP 0xc4
68#define NOTIFY_KBD_BRTDWN 0xc5
69#define NOTIFY_KBD_BRTTOGGLE 0xc7
70#define NOTIFY_KBD_FBM 0x99
71#define NOTIFY_KBD_TTP 0xae
72#define NOTIFY_LID_FLIP 0xfa
73#define NOTIFY_LID_FLIP_ROG 0xbd
74
75#define ASUS_WMI_FNLOCK_BIOS_DISABLED BIT(0)
76
77#define ASUS_MID_FAN_DESC "mid_fan"
78#define ASUS_GPU_FAN_DESC "gpu_fan"
79#define ASUS_FAN_DESC "cpu_fan"
80#define ASUS_FAN_MFUN 0x13
81#define ASUS_FAN_SFUN_READ 0x06
82#define ASUS_FAN_SFUN_WRITE 0x07
83
84/* Based on standard hwmon pwmX_enable values */
85#define ASUS_FAN_CTRL_FULLSPEED 0
86#define ASUS_FAN_CTRL_MANUAL 1
87#define ASUS_FAN_CTRL_AUTO 2
88
89#define ASUS_FAN_BOOST_MODE_NORMAL 0
90#define ASUS_FAN_BOOST_MODE_OVERBOOST 1
91#define ASUS_FAN_BOOST_MODE_OVERBOOST_MASK 0x01
92#define ASUS_FAN_BOOST_MODE_SILENT 2
93#define ASUS_FAN_BOOST_MODE_SILENT_MASK 0x02
94#define ASUS_FAN_BOOST_MODES_MASK 0x03
95
96#define ASUS_THROTTLE_THERMAL_POLICY_DEFAULT 0
97#define ASUS_THROTTLE_THERMAL_POLICY_OVERBOOST 1
98#define ASUS_THROTTLE_THERMAL_POLICY_SILENT 2
99
100#define USB_INTEL_XUSB2PR 0xD0
101#define PCI_DEVICE_ID_INTEL_LYNXPOINT_LP_XHCI 0x9c31
102
103#define ASUS_ACPI_UID_ASUSWMI "ASUSWMI"
104#define ASUS_ACPI_UID_ATK "ATK"
105
106#define WMI_EVENT_QUEUE_SIZE 0x10
107#define WMI_EVENT_QUEUE_END 0x1
108#define WMI_EVENT_MASK 0xFFFF
109/* The WMI hotkey event value is always the same. */
110#define WMI_EVENT_VALUE_ATK 0xFF
111
112#define WMI_EVENT_MASK 0xFFFF
113
114#define FAN_CURVE_POINTS 8
115#define FAN_CURVE_BUF_LEN 32
116#define FAN_CURVE_DEV_CPU 0x00
117#define FAN_CURVE_DEV_GPU 0x01
118#define FAN_CURVE_DEV_MID 0x02
119/* Mask to determine if setting temperature or percentage */
120#define FAN_CURVE_PWM_MASK 0x04
121
122/* Limits for tunables available on ASUS ROG laptops */
123#define PPT_TOTAL_MIN 5
124#define PPT_TOTAL_MAX 250
125#define PPT_CPU_MIN 5
126#define PPT_CPU_MAX 130
127#define NVIDIA_BOOST_MIN 5
128#define NVIDIA_BOOST_MAX 25
129#define NVIDIA_TEMP_MIN 75
130#define NVIDIA_TEMP_MAX 87
131
132#define ASUS_SCREENPAD_BRIGHT_MIN 20
133#define ASUS_SCREENPAD_BRIGHT_MAX 255
134#define ASUS_SCREENPAD_BRIGHT_DEFAULT 60
135
136/* Controls the power state of the USB0 hub on ROG Ally which input is on */
137#define ASUS_USB0_PWR_EC0_CSEE "\\_SB.PCI0.SBRG.EC0.CSEE"
138/* 300ms so far seems to produce a reliable result on AC and battery */
139#define ASUS_USB0_PWR_EC0_CSEE_WAIT 300
140
141static const char * const ashs_ids[] = { "ATK4001", "ATK4002", NULL };
142
143static int throttle_thermal_policy_write(struct asus_wmi *);
144
145static bool ashs_present(void)
146{
147 int i = 0;
148 while (ashs_ids[i]) {
149 if (acpi_dev_found(ashs_ids[i++]))
150 return true;
151 }
152 return false;
153}
154
155struct bios_args {
156 u32 arg0;
157 u32 arg1;
158 u32 arg2; /* At least TUF Gaming series uses 3 dword input buffer. */
159 u32 arg3;
160 u32 arg4; /* Some ROG laptops require a full 5 input args */
161 u32 arg5;
162} __packed;
163
164/*
165 * Struct that's used for all methods called via AGFN. Naming is
166 * identically to the AML code.
167 */
168struct agfn_args {
169 u16 mfun; /* probably "Multi-function" to be called */
170 u16 sfun; /* probably "Sub-function" to be called */
171 u16 len; /* size of the hole struct, including subfunction fields */
172 u8 stas; /* not used by now */
173 u8 err; /* zero on success */
174} __packed;
175
176/* struct used for calling fan read and write methods */
177struct agfn_fan_args {
178 struct agfn_args agfn; /* common fields */
179 u8 fan; /* fan number: 0: set auto mode 1: 1st fan */
180 u32 speed; /* read: RPM/100 - write: 0-255 */
181} __packed;
182
183/*
184 * <platform>/ - debugfs root directory
185 * dev_id - current dev_id
186 * ctrl_param - current ctrl_param
187 * method_id - current method_id
188 * devs - call DEVS(dev_id, ctrl_param) and print result
189 * dsts - call DSTS(dev_id) and print result
190 * call - call method_id(dev_id, ctrl_param) and print result
191 */
192struct asus_wmi_debug {
193 struct dentry *root;
194 u32 method_id;
195 u32 dev_id;
196 u32 ctrl_param;
197};
198
199struct asus_rfkill {
200 struct asus_wmi *asus;
201 struct rfkill *rfkill;
202 u32 dev_id;
203};
204
205enum fan_type {
206 FAN_TYPE_NONE = 0,
207 FAN_TYPE_AGFN, /* deprecated on newer platforms */
208 FAN_TYPE_SPEC83, /* starting in Spec 8.3, use CPU_FAN_CTRL */
209};
210
211struct fan_curve_data {
212 bool enabled;
213 u32 device_id;
214 u8 temps[FAN_CURVE_POINTS];
215 u8 percents[FAN_CURVE_POINTS];
216};
217
218struct asus_wmi {
219 int dsts_id;
220 int spec;
221 int sfun;
222 bool wmi_event_queue;
223
224 struct input_dev *inputdev;
225 struct backlight_device *backlight_device;
226 struct backlight_device *screenpad_backlight_device;
227 struct platform_device *platform_device;
228
229 struct led_classdev wlan_led;
230 int wlan_led_wk;
231 struct led_classdev tpd_led;
232 int tpd_led_wk;
233 struct led_classdev kbd_led;
234 int kbd_led_wk;
235 struct led_classdev lightbar_led;
236 int lightbar_led_wk;
237 struct led_classdev micmute_led;
238 struct workqueue_struct *led_workqueue;
239 struct work_struct tpd_led_work;
240 struct work_struct wlan_led_work;
241 struct work_struct lightbar_led_work;
242
243 struct asus_rfkill wlan;
244 struct asus_rfkill bluetooth;
245 struct asus_rfkill wimax;
246 struct asus_rfkill wwan3g;
247 struct asus_rfkill gps;
248 struct asus_rfkill uwb;
249
250 int tablet_switch_event_code;
251 u32 tablet_switch_dev_id;
252 bool tablet_switch_inverted;
253
254 enum fan_type fan_type;
255 enum fan_type gpu_fan_type;
256 enum fan_type mid_fan_type;
257 int fan_pwm_mode;
258 int gpu_fan_pwm_mode;
259 int mid_fan_pwm_mode;
260 int agfn_pwm;
261
262 bool fan_boost_mode_available;
263 u8 fan_boost_mode_mask;
264 u8 fan_boost_mode;
265
266 bool charge_mode_available;
267 bool egpu_enable_available;
268 bool egpu_connect_available;
269 bool dgpu_disable_available;
270 bool gpu_mux_mode_available;
271
272 /* Tunables provided by ASUS for gaming laptops */
273 bool ppt_pl2_sppt_available;
274 bool ppt_pl1_spl_available;
275 bool ppt_apu_sppt_available;
276 bool ppt_plat_sppt_available;
277 bool ppt_fppt_available;
278 bool nv_dyn_boost_available;
279 bool nv_temp_tgt_available;
280
281 bool kbd_rgb_mode_available;
282 bool kbd_rgb_state_available;
283
284 bool throttle_thermal_policy_available;
285 u8 throttle_thermal_policy_mode;
286
287 bool cpu_fan_curve_available;
288 bool gpu_fan_curve_available;
289 bool mid_fan_curve_available;
290 struct fan_curve_data custom_fan_curves[3];
291
292 struct platform_profile_handler platform_profile_handler;
293 bool platform_profile_support;
294
295 // The RSOC controls the maximum charging percentage.
296 bool battery_rsoc_available;
297
298 bool panel_overdrive_available;
299 bool mini_led_mode_available;
300
301 struct hotplug_slot hotplug_slot;
302 struct mutex hotplug_lock;
303 struct mutex wmi_lock;
304 struct workqueue_struct *hotplug_workqueue;
305 struct work_struct hotplug_work;
306
307 bool fnlock_locked;
308
309 /* The ROG Ally device requires the MCU USB device be disconnected before suspend */
310 bool ally_mcu_usb_switch;
311
312 struct asus_wmi_debug debug;
313
314 struct asus_wmi_driver *driver;
315};
316
317/* WMI ************************************************************************/
318
319static int asus_wmi_evaluate_method3(u32 method_id,
320 u32 arg0, u32 arg1, u32 arg2, u32 *retval)
321{
322 struct bios_args args = {
323 .arg0 = arg0,
324 .arg1 = arg1,
325 .arg2 = arg2,
326 };
327 struct acpi_buffer input = { (acpi_size) sizeof(args), &args };
328 struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER, NULL };
329 acpi_status status;
330 union acpi_object *obj;
331 u32 tmp = 0;
332
333 status = wmi_evaluate_method(ASUS_WMI_MGMT_GUID, 0, method_id,
334 &input, &output);
335
336 if (ACPI_FAILURE(status))
337 return -EIO;
338
339 obj = (union acpi_object *)output.pointer;
340 if (obj && obj->type == ACPI_TYPE_INTEGER)
341 tmp = (u32) obj->integer.value;
342
343 if (retval)
344 *retval = tmp;
345
346 kfree(obj);
347
348 if (tmp == ASUS_WMI_UNSUPPORTED_METHOD)
349 return -ENODEV;
350
351 return 0;
352}
353
354int asus_wmi_evaluate_method(u32 method_id, u32 arg0, u32 arg1, u32 *retval)
355{
356 return asus_wmi_evaluate_method3(method_id, arg0, arg1, 0, retval);
357}
358EXPORT_SYMBOL_GPL(asus_wmi_evaluate_method);
359
360static int asus_wmi_evaluate_method5(u32 method_id,
361 u32 arg0, u32 arg1, u32 arg2, u32 arg3, u32 arg4, u32 *retval)
362{
363 struct bios_args args = {
364 .arg0 = arg0,
365 .arg1 = arg1,
366 .arg2 = arg2,
367 .arg3 = arg3,
368 .arg4 = arg4,
369 };
370 struct acpi_buffer input = { (acpi_size) sizeof(args), &args };
371 struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER, NULL };
372 acpi_status status;
373 union acpi_object *obj;
374 u32 tmp = 0;
375
376 status = wmi_evaluate_method(ASUS_WMI_MGMT_GUID, 0, method_id,
377 &input, &output);
378
379 if (ACPI_FAILURE(status))
380 return -EIO;
381
382 obj = (union acpi_object *)output.pointer;
383 if (obj && obj->type == ACPI_TYPE_INTEGER)
384 tmp = (u32) obj->integer.value;
385
386 if (retval)
387 *retval = tmp;
388
389 kfree(obj);
390
391 if (tmp == ASUS_WMI_UNSUPPORTED_METHOD)
392 return -ENODEV;
393
394 return 0;
395}
396
397/*
398 * Returns as an error if the method output is not a buffer. Typically this
399 * means that the method called is unsupported.
400 */
401static int asus_wmi_evaluate_method_buf(u32 method_id,
402 u32 arg0, u32 arg1, u8 *ret_buffer, size_t size)
403{
404 struct bios_args args = {
405 .arg0 = arg0,
406 .arg1 = arg1,
407 .arg2 = 0,
408 };
409 struct acpi_buffer input = { (acpi_size) sizeof(args), &args };
410 struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER, NULL };
411 acpi_status status;
412 union acpi_object *obj;
413 int err = 0;
414
415 status = wmi_evaluate_method(ASUS_WMI_MGMT_GUID, 0, method_id,
416 &input, &output);
417
418 if (ACPI_FAILURE(status))
419 return -EIO;
420
421 obj = (union acpi_object *)output.pointer;
422
423 switch (obj->type) {
424 case ACPI_TYPE_BUFFER:
425 if (obj->buffer.length > size) {
426 err = -ENOSPC;
427 break;
428 }
429 if (obj->buffer.length == 0) {
430 err = -ENODATA;
431 break;
432 }
433
434 memcpy(ret_buffer, obj->buffer.pointer, obj->buffer.length);
435 break;
436 case ACPI_TYPE_INTEGER:
437 err = (u32)obj->integer.value;
438
439 if (err == ASUS_WMI_UNSUPPORTED_METHOD)
440 err = -ENODEV;
441 /*
442 * At least one method returns a 0 with no buffer if no arg
443 * is provided, such as ASUS_WMI_DEVID_CPU_FAN_CURVE
444 */
445 if (err == 0)
446 err = -ENODATA;
447 break;
448 default:
449 err = -ENODATA;
450 break;
451 }
452
453 kfree(obj);
454
455 if (err)
456 return err;
457
458 return 0;
459}
460
461static int asus_wmi_evaluate_method_agfn(const struct acpi_buffer args)
462{
463 struct acpi_buffer input;
464 u64 phys_addr;
465 u32 retval;
466 u32 status;
467
468 /*
469 * Copy to dma capable address otherwise memory corruption occurs as
470 * bios has to be able to access it.
471 */
472 input.pointer = kmemdup(args.pointer, args.length, GFP_DMA | GFP_KERNEL);
473 input.length = args.length;
474 if (!input.pointer)
475 return -ENOMEM;
476 phys_addr = virt_to_phys(input.pointer);
477
478 status = asus_wmi_evaluate_method(ASUS_WMI_METHODID_AGFN,
479 phys_addr, 0, &retval);
480 if (!status)
481 memcpy(args.pointer, input.pointer, args.length);
482
483 kfree(input.pointer);
484 if (status)
485 return -ENXIO;
486
487 return retval;
488}
489
490static int asus_wmi_get_devstate(struct asus_wmi *asus, u32 dev_id, u32 *retval)
491{
492 return asus_wmi_evaluate_method(asus->dsts_id, dev_id, 0, retval);
493}
494
495static int asus_wmi_set_devstate(u32 dev_id, u32 ctrl_param,
496 u32 *retval)
497{
498 return asus_wmi_evaluate_method(ASUS_WMI_METHODID_DEVS, dev_id,
499 ctrl_param, retval);
500}
501
502/* Helper for special devices with magic return codes */
503static int asus_wmi_get_devstate_bits(struct asus_wmi *asus,
504 u32 dev_id, u32 mask)
505{
506 u32 retval = 0;
507 int err;
508
509 err = asus_wmi_get_devstate(asus, dev_id, &retval);
510 if (err < 0)
511 return err;
512
513 if (!(retval & ASUS_WMI_DSTS_PRESENCE_BIT))
514 return -ENODEV;
515
516 if (mask == ASUS_WMI_DSTS_STATUS_BIT) {
517 if (retval & ASUS_WMI_DSTS_UNKNOWN_BIT)
518 return -ENODEV;
519 }
520
521 return retval & mask;
522}
523
524static int asus_wmi_get_devstate_simple(struct asus_wmi *asus, u32 dev_id)
525{
526 return asus_wmi_get_devstate_bits(asus, dev_id,
527 ASUS_WMI_DSTS_STATUS_BIT);
528}
529
530static bool asus_wmi_dev_is_present(struct asus_wmi *asus, u32 dev_id)
531{
532 u32 retval;
533 int status = asus_wmi_get_devstate(asus, dev_id, &retval);
534
535 return status == 0 && (retval & ASUS_WMI_DSTS_PRESENCE_BIT);
536}
537
538/* Input **********************************************************************/
539static void asus_wmi_tablet_sw_report(struct asus_wmi *asus, bool value)
540{
541 input_report_switch(asus->inputdev, SW_TABLET_MODE,
542 asus->tablet_switch_inverted ? !value : value);
543 input_sync(asus->inputdev);
544}
545
546static void asus_wmi_tablet_sw_init(struct asus_wmi *asus, u32 dev_id, int event_code)
547{
548 struct device *dev = &asus->platform_device->dev;
549 int result;
550
551 result = asus_wmi_get_devstate_simple(asus, dev_id);
552 if (result >= 0) {
553 input_set_capability(asus->inputdev, EV_SW, SW_TABLET_MODE);
554 asus_wmi_tablet_sw_report(asus, result);
555 asus->tablet_switch_dev_id = dev_id;
556 asus->tablet_switch_event_code = event_code;
557 } else if (result == -ENODEV) {
558 dev_err(dev, "This device has tablet-mode-switch quirk but got ENODEV checking it. This is a bug.");
559 } else {
560 dev_err(dev, "Error checking for tablet-mode-switch: %d\n", result);
561 }
562}
563
564static int asus_wmi_input_init(struct asus_wmi *asus)
565{
566 struct device *dev = &asus->platform_device->dev;
567 int err;
568
569 asus->inputdev = input_allocate_device();
570 if (!asus->inputdev)
571 return -ENOMEM;
572
573 asus->inputdev->name = asus->driver->input_name;
574 asus->inputdev->phys = asus->driver->input_phys;
575 asus->inputdev->id.bustype = BUS_HOST;
576 asus->inputdev->dev.parent = dev;
577 set_bit(EV_REP, asus->inputdev->evbit);
578
579 err = sparse_keymap_setup(asus->inputdev, asus->driver->keymap, NULL);
580 if (err)
581 goto err_free_dev;
582
583 switch (asus->driver->quirks->tablet_switch_mode) {
584 case asus_wmi_no_tablet_switch:
585 break;
586 case asus_wmi_kbd_dock_devid:
587 asus->tablet_switch_inverted = true;
588 asus_wmi_tablet_sw_init(asus, ASUS_WMI_DEVID_KBD_DOCK, NOTIFY_KBD_DOCK_CHANGE);
589 break;
590 case asus_wmi_lid_flip_devid:
591 asus_wmi_tablet_sw_init(asus, ASUS_WMI_DEVID_LID_FLIP, NOTIFY_LID_FLIP);
592 break;
593 case asus_wmi_lid_flip_rog_devid:
594 asus_wmi_tablet_sw_init(asus, ASUS_WMI_DEVID_LID_FLIP_ROG, NOTIFY_LID_FLIP_ROG);
595 break;
596 }
597
598 err = input_register_device(asus->inputdev);
599 if (err)
600 goto err_free_dev;
601
602 return 0;
603
604err_free_dev:
605 input_free_device(asus->inputdev);
606 return err;
607}
608
609static void asus_wmi_input_exit(struct asus_wmi *asus)
610{
611 if (asus->inputdev)
612 input_unregister_device(asus->inputdev);
613
614 asus->inputdev = NULL;
615}
616
617/* Tablet mode ****************************************************************/
618
619static void asus_wmi_tablet_mode_get_state(struct asus_wmi *asus)
620{
621 int result;
622
623 if (!asus->tablet_switch_dev_id)
624 return;
625
626 result = asus_wmi_get_devstate_simple(asus, asus->tablet_switch_dev_id);
627 if (result >= 0)
628 asus_wmi_tablet_sw_report(asus, result);
629}
630
631/* Charging mode, 1=Barrel, 2=USB ******************************************/
632static ssize_t charge_mode_show(struct device *dev,
633 struct device_attribute *attr, char *buf)
634{
635 struct asus_wmi *asus = dev_get_drvdata(dev);
636 int result, value;
637
638 result = asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_CHARGE_MODE, &value);
639 if (result < 0)
640 return result;
641
642 return sysfs_emit(buf, "%d\n", value & 0xff);
643}
644
645static DEVICE_ATTR_RO(charge_mode);
646
647/* dGPU ********************************************************************/
648static ssize_t dgpu_disable_show(struct device *dev,
649 struct device_attribute *attr, char *buf)
650{
651 struct asus_wmi *asus = dev_get_drvdata(dev);
652 int result;
653
654 result = asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_DGPU);
655 if (result < 0)
656 return result;
657
658 return sysfs_emit(buf, "%d\n", result);
659}
660
661/*
662 * A user may be required to store the value twice, typcial store first, then
663 * rescan PCI bus to activate power, then store a second time to save correctly.
664 * The reason for this is that an extra code path in the ACPI is enabled when
665 * the device and bus are powered.
666 */
667static ssize_t dgpu_disable_store(struct device *dev,
668 struct device_attribute *attr,
669 const char *buf, size_t count)
670{
671 int result, err;
672 u32 disable;
673
674 struct asus_wmi *asus = dev_get_drvdata(dev);
675
676 result = kstrtou32(buf, 10, &disable);
677 if (result)
678 return result;
679
680 if (disable > 1)
681 return -EINVAL;
682
683 if (asus->gpu_mux_mode_available) {
684 result = asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_GPU_MUX);
685 if (result < 0)
686 /* An error here may signal greater failure of GPU handling */
687 return result;
688 if (!result && disable) {
689 err = -ENODEV;
690 pr_warn("Can not disable dGPU when the MUX is in dGPU mode: %d\n", err);
691 return err;
692 }
693 }
694
695 err = asus_wmi_set_devstate(ASUS_WMI_DEVID_DGPU, disable, &result);
696 if (err) {
697 pr_warn("Failed to set dgpu disable: %d\n", err);
698 return err;
699 }
700
701 if (result > 1) {
702 pr_warn("Failed to set dgpu disable (result): 0x%x\n", result);
703 return -EIO;
704 }
705
706 sysfs_notify(&asus->platform_device->dev.kobj, NULL, "dgpu_disable");
707
708 return count;
709}
710static DEVICE_ATTR_RW(dgpu_disable);
711
712/* eGPU ********************************************************************/
713static ssize_t egpu_enable_show(struct device *dev,
714 struct device_attribute *attr, char *buf)
715{
716 struct asus_wmi *asus = dev_get_drvdata(dev);
717 int result;
718
719 result = asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_EGPU);
720 if (result < 0)
721 return result;
722
723 return sysfs_emit(buf, "%d\n", result);
724}
725
726/* The ACPI call to enable the eGPU also disables the internal dGPU */
727static ssize_t egpu_enable_store(struct device *dev,
728 struct device_attribute *attr,
729 const char *buf, size_t count)
730{
731 int result, err;
732 u32 enable;
733
734 struct asus_wmi *asus = dev_get_drvdata(dev);
735
736 err = kstrtou32(buf, 10, &enable);
737 if (err)
738 return err;
739
740 if (enable > 1)
741 return -EINVAL;
742
743 err = asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_EGPU_CONNECTED);
744 if (err < 0) {
745 pr_warn("Failed to get egpu connection status: %d\n", err);
746 return err;
747 }
748
749 if (asus->gpu_mux_mode_available) {
750 result = asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_GPU_MUX);
751 if (result < 0) {
752 /* An error here may signal greater failure of GPU handling */
753 pr_warn("Failed to get gpu mux status: %d\n", result);
754 return result;
755 }
756 if (!result && enable) {
757 err = -ENODEV;
758 pr_warn("Can not enable eGPU when the MUX is in dGPU mode: %d\n", err);
759 return err;
760 }
761 }
762
763 err = asus_wmi_set_devstate(ASUS_WMI_DEVID_EGPU, enable, &result);
764 if (err) {
765 pr_warn("Failed to set egpu state: %d\n", err);
766 return err;
767 }
768
769 if (result > 1) {
770 pr_warn("Failed to set egpu state (retval): 0x%x\n", result);
771 return -EIO;
772 }
773
774 sysfs_notify(&asus->platform_device->dev.kobj, NULL, "egpu_enable");
775
776 return count;
777}
778static DEVICE_ATTR_RW(egpu_enable);
779
780/* Is eGPU connected? *********************************************************/
781static ssize_t egpu_connected_show(struct device *dev,
782 struct device_attribute *attr, char *buf)
783{
784 struct asus_wmi *asus = dev_get_drvdata(dev);
785 int result;
786
787 result = asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_EGPU_CONNECTED);
788 if (result < 0)
789 return result;
790
791 return sysfs_emit(buf, "%d\n", result);
792}
793
794static DEVICE_ATTR_RO(egpu_connected);
795
796/* gpu mux switch *************************************************************/
797static ssize_t gpu_mux_mode_show(struct device *dev,
798 struct device_attribute *attr, char *buf)
799{
800 struct asus_wmi *asus = dev_get_drvdata(dev);
801 int result;
802
803 result = asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_GPU_MUX);
804 if (result < 0)
805 return result;
806
807 return sysfs_emit(buf, "%d\n", result);
808}
809
810static ssize_t gpu_mux_mode_store(struct device *dev,
811 struct device_attribute *attr,
812 const char *buf, size_t count)
813{
814 struct asus_wmi *asus = dev_get_drvdata(dev);
815 int result, err;
816 u32 optimus;
817
818 err = kstrtou32(buf, 10, &optimus);
819 if (err)
820 return err;
821
822 if (optimus > 1)
823 return -EINVAL;
824
825 if (asus->dgpu_disable_available) {
826 result = asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_DGPU);
827 if (result < 0)
828 /* An error here may signal greater failure of GPU handling */
829 return result;
830 if (result && !optimus) {
831 err = -ENODEV;
832 pr_warn("Can not switch MUX to dGPU mode when dGPU is disabled: %d\n", err);
833 return err;
834 }
835 }
836
837 if (asus->egpu_enable_available) {
838 result = asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_EGPU);
839 if (result < 0)
840 /* An error here may signal greater failure of GPU handling */
841 return result;
842 if (result && !optimus) {
843 err = -ENODEV;
844 pr_warn("Can not switch MUX to dGPU mode when eGPU is enabled: %d\n", err);
845 return err;
846 }
847 }
848
849 err = asus_wmi_set_devstate(ASUS_WMI_DEVID_GPU_MUX, optimus, &result);
850 if (err) {
851 dev_err(dev, "Failed to set GPU MUX mode: %d\n", err);
852 return err;
853 }
854 /* !1 is considered a fail by ASUS */
855 if (result != 1) {
856 dev_warn(dev, "Failed to set GPU MUX mode (result): 0x%x\n", result);
857 return -EIO;
858 }
859
860 sysfs_notify(&asus->platform_device->dev.kobj, NULL, "gpu_mux_mode");
861
862 return count;
863}
864static DEVICE_ATTR_RW(gpu_mux_mode);
865
866/* TUF Laptop Keyboard RGB Modes **********************************************/
867static ssize_t kbd_rgb_mode_store(struct device *dev,
868 struct device_attribute *attr,
869 const char *buf, size_t count)
870{
871 u32 cmd, mode, r, g, b, speed;
872 int err;
873
874 if (sscanf(buf, "%d %d %d %d %d %d", &cmd, &mode, &r, &g, &b, &speed) != 6)
875 return -EINVAL;
876
877 /* B3 is set and B4 is save to BIOS */
878 switch (cmd) {
879 case 0:
880 cmd = 0xb3;
881 break;
882 case 1:
883 cmd = 0xb4;
884 break;
885 default:
886 return -EINVAL;
887 }
888
889 /* These are the known usable modes across all TUF/ROG */
890 if (mode >= 12 || mode == 9)
891 mode = 10;
892
893 switch (speed) {
894 case 0:
895 speed = 0xe1;
896 break;
897 case 1:
898 speed = 0xeb;
899 break;
900 case 2:
901 speed = 0xf5;
902 break;
903 default:
904 speed = 0xeb;
905 }
906
907 err = asus_wmi_evaluate_method3(ASUS_WMI_METHODID_DEVS, ASUS_WMI_DEVID_TUF_RGB_MODE,
908 cmd | (mode << 8) | (r << 16) | (g << 24), b | (speed << 8), NULL);
909 if (err)
910 return err;
911
912 return count;
913}
914static DEVICE_ATTR_WO(kbd_rgb_mode);
915
916static ssize_t kbd_rgb_mode_index_show(struct device *device,
917 struct device_attribute *attr,
918 char *buf)
919{
920 return sysfs_emit(buf, "%s\n", "cmd mode red green blue speed");
921}
922static DEVICE_ATTR_RO(kbd_rgb_mode_index);
923
924static struct attribute *kbd_rgb_mode_attrs[] = {
925 &dev_attr_kbd_rgb_mode.attr,
926 &dev_attr_kbd_rgb_mode_index.attr,
927 NULL,
928};
929
930static const struct attribute_group kbd_rgb_mode_group = {
931 .attrs = kbd_rgb_mode_attrs,
932};
933
934/* TUF Laptop Keyboard RGB State **********************************************/
935static ssize_t kbd_rgb_state_store(struct device *dev,
936 struct device_attribute *attr,
937 const char *buf, size_t count)
938{
939 u32 flags, cmd, boot, awake, sleep, keyboard;
940 int err;
941
942 if (sscanf(buf, "%d %d %d %d %d", &cmd, &boot, &awake, &sleep, &keyboard) != 5)
943 return -EINVAL;
944
945 if (cmd)
946 cmd = BIT(2);
947
948 flags = 0;
949 if (boot)
950 flags |= BIT(1);
951 if (awake)
952 flags |= BIT(3);
953 if (sleep)
954 flags |= BIT(5);
955 if (keyboard)
956 flags |= BIT(7);
957
958 /* 0xbd is the required default arg0 for the method. Nothing happens otherwise */
959 err = asus_wmi_evaluate_method3(ASUS_WMI_METHODID_DEVS,
960 ASUS_WMI_DEVID_TUF_RGB_STATE, 0xbd | cmd << 8 | (flags << 16), 0, NULL);
961 if (err)
962 return err;
963
964 return count;
965}
966static DEVICE_ATTR_WO(kbd_rgb_state);
967
968static ssize_t kbd_rgb_state_index_show(struct device *device,
969 struct device_attribute *attr,
970 char *buf)
971{
972 return sysfs_emit(buf, "%s\n", "cmd boot awake sleep keyboard");
973}
974static DEVICE_ATTR_RO(kbd_rgb_state_index);
975
976static struct attribute *kbd_rgb_state_attrs[] = {
977 &dev_attr_kbd_rgb_state.attr,
978 &dev_attr_kbd_rgb_state_index.attr,
979 NULL,
980};
981
982static const struct attribute_group kbd_rgb_state_group = {
983 .attrs = kbd_rgb_state_attrs,
984};
985
986static const struct attribute_group *kbd_rgb_mode_groups[] = {
987 NULL,
988 NULL,
989 NULL,
990};
991
992/* Tunable: PPT: Intel=PL1, AMD=SPPT *****************************************/
993static ssize_t ppt_pl2_sppt_store(struct device *dev,
994 struct device_attribute *attr,
995 const char *buf, size_t count)
996{
997 int result, err;
998 u32 value;
999
1000 struct asus_wmi *asus = dev_get_drvdata(dev);
1001
1002 result = kstrtou32(buf, 10, &value);
1003 if (result)
1004 return result;
1005
1006 if (value < PPT_TOTAL_MIN || value > PPT_TOTAL_MAX)
1007 return -EINVAL;
1008
1009 err = asus_wmi_set_devstate(ASUS_WMI_DEVID_PPT_PL2_SPPT, value, &result);
1010 if (err) {
1011 pr_warn("Failed to set ppt_pl2_sppt: %d\n", err);
1012 return err;
1013 }
1014
1015 if (result > 1) {
1016 pr_warn("Failed to set ppt_pl2_sppt (result): 0x%x\n", result);
1017 return -EIO;
1018 }
1019
1020 sysfs_notify(&asus->platform_device->dev.kobj, NULL, "ppt_pl2_sppt");
1021
1022 return count;
1023}
1024static DEVICE_ATTR_WO(ppt_pl2_sppt);
1025
1026/* Tunable: PPT, Intel=PL1, AMD=SPL ******************************************/
1027static ssize_t ppt_pl1_spl_store(struct device *dev,
1028 struct device_attribute *attr,
1029 const char *buf, size_t count)
1030{
1031 int result, err;
1032 u32 value;
1033
1034 struct asus_wmi *asus = dev_get_drvdata(dev);
1035
1036 result = kstrtou32(buf, 10, &value);
1037 if (result)
1038 return result;
1039
1040 if (value < PPT_TOTAL_MIN || value > PPT_TOTAL_MAX)
1041 return -EINVAL;
1042
1043 err = asus_wmi_set_devstate(ASUS_WMI_DEVID_PPT_PL1_SPL, value, &result);
1044 if (err) {
1045 pr_warn("Failed to set ppt_pl1_spl: %d\n", err);
1046 return err;
1047 }
1048
1049 if (result > 1) {
1050 pr_warn("Failed to set ppt_pl1_spl (result): 0x%x\n", result);
1051 return -EIO;
1052 }
1053
1054 sysfs_notify(&asus->platform_device->dev.kobj, NULL, "ppt_pl1_spl");
1055
1056 return count;
1057}
1058static DEVICE_ATTR_WO(ppt_pl1_spl);
1059
1060/* Tunable: PPT APU FPPT ******************************************************/
1061static ssize_t ppt_fppt_store(struct device *dev,
1062 struct device_attribute *attr,
1063 const char *buf, size_t count)
1064{
1065 int result, err;
1066 u32 value;
1067
1068 struct asus_wmi *asus = dev_get_drvdata(dev);
1069
1070 result = kstrtou32(buf, 10, &value);
1071 if (result)
1072 return result;
1073
1074 if (value < PPT_TOTAL_MIN || value > PPT_TOTAL_MAX)
1075 return -EINVAL;
1076
1077 err = asus_wmi_set_devstate(ASUS_WMI_DEVID_PPT_FPPT, value, &result);
1078 if (err) {
1079 pr_warn("Failed to set ppt_fppt: %d\n", err);
1080 return err;
1081 }
1082
1083 if (result > 1) {
1084 pr_warn("Failed to set ppt_fppt (result): 0x%x\n", result);
1085 return -EIO;
1086 }
1087
1088 sysfs_notify(&asus->platform_device->dev.kobj, NULL, "ppt_fpu_sppt");
1089
1090 return count;
1091}
1092static DEVICE_ATTR_WO(ppt_fppt);
1093
1094/* Tunable: PPT APU SPPT *****************************************************/
1095static ssize_t ppt_apu_sppt_store(struct device *dev,
1096 struct device_attribute *attr,
1097 const char *buf, size_t count)
1098{
1099 int result, err;
1100 u32 value;
1101
1102 struct asus_wmi *asus = dev_get_drvdata(dev);
1103
1104 result = kstrtou32(buf, 10, &value);
1105 if (result)
1106 return result;
1107
1108 if (value < PPT_CPU_MIN || value > PPT_CPU_MAX)
1109 return -EINVAL;
1110
1111 err = asus_wmi_set_devstate(ASUS_WMI_DEVID_PPT_APU_SPPT, value, &result);
1112 if (err) {
1113 pr_warn("Failed to set ppt_apu_sppt: %d\n", err);
1114 return err;
1115 }
1116
1117 if (result > 1) {
1118 pr_warn("Failed to set ppt_apu_sppt (result): 0x%x\n", result);
1119 return -EIO;
1120 }
1121
1122 sysfs_notify(&asus->platform_device->dev.kobj, NULL, "ppt_apu_sppt");
1123
1124 return count;
1125}
1126static DEVICE_ATTR_WO(ppt_apu_sppt);
1127
1128/* Tunable: PPT platform SPPT ************************************************/
1129static ssize_t ppt_platform_sppt_store(struct device *dev,
1130 struct device_attribute *attr,
1131 const char *buf, size_t count)
1132{
1133 int result, err;
1134 u32 value;
1135
1136 struct asus_wmi *asus = dev_get_drvdata(dev);
1137
1138 result = kstrtou32(buf, 10, &value);
1139 if (result)
1140 return result;
1141
1142 if (value < PPT_CPU_MIN || value > PPT_CPU_MAX)
1143 return -EINVAL;
1144
1145 err = asus_wmi_set_devstate(ASUS_WMI_DEVID_PPT_PLAT_SPPT, value, &result);
1146 if (err) {
1147 pr_warn("Failed to set ppt_platform_sppt: %d\n", err);
1148 return err;
1149 }
1150
1151 if (result > 1) {
1152 pr_warn("Failed to set ppt_platform_sppt (result): 0x%x\n", result);
1153 return -EIO;
1154 }
1155
1156 sysfs_notify(&asus->platform_device->dev.kobj, NULL, "ppt_platform_sppt");
1157
1158 return count;
1159}
1160static DEVICE_ATTR_WO(ppt_platform_sppt);
1161
1162/* Tunable: NVIDIA dynamic boost *********************************************/
1163static ssize_t nv_dynamic_boost_store(struct device *dev,
1164 struct device_attribute *attr,
1165 const char *buf, size_t count)
1166{
1167 int result, err;
1168 u32 value;
1169
1170 struct asus_wmi *asus = dev_get_drvdata(dev);
1171
1172 result = kstrtou32(buf, 10, &value);
1173 if (result)
1174 return result;
1175
1176 if (value < NVIDIA_BOOST_MIN || value > NVIDIA_BOOST_MAX)
1177 return -EINVAL;
1178
1179 err = asus_wmi_set_devstate(ASUS_WMI_DEVID_NV_DYN_BOOST, value, &result);
1180 if (err) {
1181 pr_warn("Failed to set nv_dynamic_boost: %d\n", err);
1182 return err;
1183 }
1184
1185 if (result > 1) {
1186 pr_warn("Failed to set nv_dynamic_boost (result): 0x%x\n", result);
1187 return -EIO;
1188 }
1189
1190 sysfs_notify(&asus->platform_device->dev.kobj, NULL, "nv_dynamic_boost");
1191
1192 return count;
1193}
1194static DEVICE_ATTR_WO(nv_dynamic_boost);
1195
1196/* Tunable: NVIDIA temperature target ****************************************/
1197static ssize_t nv_temp_target_store(struct device *dev,
1198 struct device_attribute *attr,
1199 const char *buf, size_t count)
1200{
1201 int result, err;
1202 u32 value;
1203
1204 struct asus_wmi *asus = dev_get_drvdata(dev);
1205
1206 result = kstrtou32(buf, 10, &value);
1207 if (result)
1208 return result;
1209
1210 if (value < NVIDIA_TEMP_MIN || value > NVIDIA_TEMP_MAX)
1211 return -EINVAL;
1212
1213 err = asus_wmi_set_devstate(ASUS_WMI_DEVID_NV_THERM_TARGET, value, &result);
1214 if (err) {
1215 pr_warn("Failed to set nv_temp_target: %d\n", err);
1216 return err;
1217 }
1218
1219 if (result > 1) {
1220 pr_warn("Failed to set nv_temp_target (result): 0x%x\n", result);
1221 return -EIO;
1222 }
1223
1224 sysfs_notify(&asus->platform_device->dev.kobj, NULL, "nv_temp_target");
1225
1226 return count;
1227}
1228static DEVICE_ATTR_WO(nv_temp_target);
1229
1230/* Battery ********************************************************************/
1231
1232/* The battery maximum charging percentage */
1233static int charge_end_threshold;
1234
1235static ssize_t charge_control_end_threshold_store(struct device *dev,
1236 struct device_attribute *attr,
1237 const char *buf, size_t count)
1238{
1239 int value, ret, rv;
1240
1241 ret = kstrtouint(buf, 10, &value);
1242 if (ret)
1243 return ret;
1244
1245 if (value < 0 || value > 100)
1246 return -EINVAL;
1247
1248 ret = asus_wmi_set_devstate(ASUS_WMI_DEVID_RSOC, value, &rv);
1249 if (ret)
1250 return ret;
1251
1252 if (rv != 1)
1253 return -EIO;
1254
1255 /* There isn't any method in the DSDT to read the threshold, so we
1256 * save the threshold.
1257 */
1258 charge_end_threshold = value;
1259 return count;
1260}
1261
1262static ssize_t charge_control_end_threshold_show(struct device *device,
1263 struct device_attribute *attr,
1264 char *buf)
1265{
1266 return sysfs_emit(buf, "%d\n", charge_end_threshold);
1267}
1268
1269static DEVICE_ATTR_RW(charge_control_end_threshold);
1270
1271static int asus_wmi_battery_add(struct power_supply *battery, struct acpi_battery_hook *hook)
1272{
1273 /* The WMI method does not provide a way to specific a battery, so we
1274 * just assume it is the first battery.
1275 * Note: On some newer ASUS laptops (Zenbook UM431DA), the primary/first
1276 * battery is named BATT.
1277 */
1278 if (strcmp(battery->desc->name, "BAT0") != 0 &&
1279 strcmp(battery->desc->name, "BAT1") != 0 &&
1280 strcmp(battery->desc->name, "BATC") != 0 &&
1281 strcmp(battery->desc->name, "BATT") != 0)
1282 return -ENODEV;
1283
1284 if (device_create_file(&battery->dev,
1285 &dev_attr_charge_control_end_threshold))
1286 return -ENODEV;
1287
1288 /* The charge threshold is only reset when the system is power cycled,
1289 * and we can't get the current threshold so let set it to 100% when
1290 * a battery is added.
1291 */
1292 asus_wmi_set_devstate(ASUS_WMI_DEVID_RSOC, 100, NULL);
1293 charge_end_threshold = 100;
1294
1295 return 0;
1296}
1297
1298static int asus_wmi_battery_remove(struct power_supply *battery, struct acpi_battery_hook *hook)
1299{
1300 device_remove_file(&battery->dev,
1301 &dev_attr_charge_control_end_threshold);
1302 return 0;
1303}
1304
1305static struct acpi_battery_hook battery_hook = {
1306 .add_battery = asus_wmi_battery_add,
1307 .remove_battery = asus_wmi_battery_remove,
1308 .name = "ASUS Battery Extension",
1309};
1310
1311static void asus_wmi_battery_init(struct asus_wmi *asus)
1312{
1313 asus->battery_rsoc_available = false;
1314 if (asus_wmi_dev_is_present(asus, ASUS_WMI_DEVID_RSOC)) {
1315 asus->battery_rsoc_available = true;
1316 battery_hook_register(&battery_hook);
1317 }
1318}
1319
1320static void asus_wmi_battery_exit(struct asus_wmi *asus)
1321{
1322 if (asus->battery_rsoc_available)
1323 battery_hook_unregister(&battery_hook);
1324}
1325
1326/* LEDs ***********************************************************************/
1327
1328/*
1329 * These functions actually update the LED's, and are called from a
1330 * workqueue. By doing this as separate work rather than when the LED
1331 * subsystem asks, we avoid messing with the Asus ACPI stuff during a
1332 * potentially bad time, such as a timer interrupt.
1333 */
1334static void tpd_led_update(struct work_struct *work)
1335{
1336 int ctrl_param;
1337 struct asus_wmi *asus;
1338
1339 asus = container_of(work, struct asus_wmi, tpd_led_work);
1340
1341 ctrl_param = asus->tpd_led_wk;
1342 asus_wmi_set_devstate(ASUS_WMI_DEVID_TOUCHPAD_LED, ctrl_param, NULL);
1343}
1344
1345static void tpd_led_set(struct led_classdev *led_cdev,
1346 enum led_brightness value)
1347{
1348 struct asus_wmi *asus;
1349
1350 asus = container_of(led_cdev, struct asus_wmi, tpd_led);
1351
1352 asus->tpd_led_wk = !!value;
1353 queue_work(asus->led_workqueue, &asus->tpd_led_work);
1354}
1355
1356static int read_tpd_led_state(struct asus_wmi *asus)
1357{
1358 return asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_TOUCHPAD_LED);
1359}
1360
1361static enum led_brightness tpd_led_get(struct led_classdev *led_cdev)
1362{
1363 struct asus_wmi *asus;
1364
1365 asus = container_of(led_cdev, struct asus_wmi, tpd_led);
1366
1367 return read_tpd_led_state(asus);
1368}
1369
1370static void kbd_led_update(struct asus_wmi *asus)
1371{
1372 int ctrl_param = 0;
1373
1374 ctrl_param = 0x80 | (asus->kbd_led_wk & 0x7F);
1375 asus_wmi_set_devstate(ASUS_WMI_DEVID_KBD_BACKLIGHT, ctrl_param, NULL);
1376}
1377
1378static int kbd_led_read(struct asus_wmi *asus, int *level, int *env)
1379{
1380 int retval;
1381
1382 /*
1383 * bits 0-2: level
1384 * bit 7: light on/off
1385 * bit 8-10: environment (0: dark, 1: normal, 2: light)
1386 * bit 17: status unknown
1387 */
1388 retval = asus_wmi_get_devstate_bits(asus, ASUS_WMI_DEVID_KBD_BACKLIGHT,
1389 0xFFFF);
1390
1391 /* Unknown status is considered as off */
1392 if (retval == 0x8000)
1393 retval = 0;
1394
1395 if (retval < 0)
1396 return retval;
1397
1398 if (level)
1399 *level = retval & 0x7F;
1400 if (env)
1401 *env = (retval >> 8) & 0x7F;
1402 return 0;
1403}
1404
1405static void do_kbd_led_set(struct led_classdev *led_cdev, int value)
1406{
1407 struct asus_wmi *asus;
1408 int max_level;
1409
1410 asus = container_of(led_cdev, struct asus_wmi, kbd_led);
1411 max_level = asus->kbd_led.max_brightness;
1412
1413 asus->kbd_led_wk = clamp_val(value, 0, max_level);
1414 kbd_led_update(asus);
1415}
1416
1417static void kbd_led_set(struct led_classdev *led_cdev,
1418 enum led_brightness value)
1419{
1420 /* Prevent disabling keyboard backlight on module unregister */
1421 if (led_cdev->flags & LED_UNREGISTERING)
1422 return;
1423
1424 do_kbd_led_set(led_cdev, value);
1425}
1426
1427static void kbd_led_set_by_kbd(struct asus_wmi *asus, enum led_brightness value)
1428{
1429 struct led_classdev *led_cdev = &asus->kbd_led;
1430
1431 do_kbd_led_set(led_cdev, value);
1432 led_classdev_notify_brightness_hw_changed(led_cdev, asus->kbd_led_wk);
1433}
1434
1435static enum led_brightness kbd_led_get(struct led_classdev *led_cdev)
1436{
1437 struct asus_wmi *asus;
1438 int retval, value;
1439
1440 asus = container_of(led_cdev, struct asus_wmi, kbd_led);
1441
1442 retval = kbd_led_read(asus, &value, NULL);
1443 if (retval < 0)
1444 return retval;
1445
1446 return value;
1447}
1448
1449static int wlan_led_unknown_state(struct asus_wmi *asus)
1450{
1451 u32 result;
1452
1453 asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_WIRELESS_LED, &result);
1454
1455 return result & ASUS_WMI_DSTS_UNKNOWN_BIT;
1456}
1457
1458static void wlan_led_update(struct work_struct *work)
1459{
1460 int ctrl_param;
1461 struct asus_wmi *asus;
1462
1463 asus = container_of(work, struct asus_wmi, wlan_led_work);
1464
1465 ctrl_param = asus->wlan_led_wk;
1466 asus_wmi_set_devstate(ASUS_WMI_DEVID_WIRELESS_LED, ctrl_param, NULL);
1467}
1468
1469static void wlan_led_set(struct led_classdev *led_cdev,
1470 enum led_brightness value)
1471{
1472 struct asus_wmi *asus;
1473
1474 asus = container_of(led_cdev, struct asus_wmi, wlan_led);
1475
1476 asus->wlan_led_wk = !!value;
1477 queue_work(asus->led_workqueue, &asus->wlan_led_work);
1478}
1479
1480static enum led_brightness wlan_led_get(struct led_classdev *led_cdev)
1481{
1482 struct asus_wmi *asus;
1483 u32 result;
1484
1485 asus = container_of(led_cdev, struct asus_wmi, wlan_led);
1486 asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_WIRELESS_LED, &result);
1487
1488 return result & ASUS_WMI_DSTS_BRIGHTNESS_MASK;
1489}
1490
1491static void lightbar_led_update(struct work_struct *work)
1492{
1493 struct asus_wmi *asus;
1494 int ctrl_param;
1495
1496 asus = container_of(work, struct asus_wmi, lightbar_led_work);
1497
1498 ctrl_param = asus->lightbar_led_wk;
1499 asus_wmi_set_devstate(ASUS_WMI_DEVID_LIGHTBAR, ctrl_param, NULL);
1500}
1501
1502static void lightbar_led_set(struct led_classdev *led_cdev,
1503 enum led_brightness value)
1504{
1505 struct asus_wmi *asus;
1506
1507 asus = container_of(led_cdev, struct asus_wmi, lightbar_led);
1508
1509 asus->lightbar_led_wk = !!value;
1510 queue_work(asus->led_workqueue, &asus->lightbar_led_work);
1511}
1512
1513static enum led_brightness lightbar_led_get(struct led_classdev *led_cdev)
1514{
1515 struct asus_wmi *asus;
1516 u32 result;
1517
1518 asus = container_of(led_cdev, struct asus_wmi, lightbar_led);
1519 asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_LIGHTBAR, &result);
1520
1521 return result & ASUS_WMI_DSTS_LIGHTBAR_MASK;
1522}
1523
1524static int micmute_led_set(struct led_classdev *led_cdev,
1525 enum led_brightness brightness)
1526{
1527 int state = brightness != LED_OFF;
1528 int err;
1529
1530 err = asus_wmi_set_devstate(ASUS_WMI_DEVID_MICMUTE_LED, state, NULL);
1531 return err < 0 ? err : 0;
1532}
1533
1534static void asus_wmi_led_exit(struct asus_wmi *asus)
1535{
1536 led_classdev_unregister(&asus->kbd_led);
1537 led_classdev_unregister(&asus->tpd_led);
1538 led_classdev_unregister(&asus->wlan_led);
1539 led_classdev_unregister(&asus->lightbar_led);
1540 led_classdev_unregister(&asus->micmute_led);
1541
1542 if (asus->led_workqueue)
1543 destroy_workqueue(asus->led_workqueue);
1544}
1545
1546static int asus_wmi_led_init(struct asus_wmi *asus)
1547{
1548 int rv = 0, num_rgb_groups = 0, led_val;
1549
1550 if (asus->kbd_rgb_mode_available)
1551 kbd_rgb_mode_groups[num_rgb_groups++] = &kbd_rgb_mode_group;
1552 if (asus->kbd_rgb_state_available)
1553 kbd_rgb_mode_groups[num_rgb_groups++] = &kbd_rgb_state_group;
1554
1555 asus->led_workqueue = create_singlethread_workqueue("led_workqueue");
1556 if (!asus->led_workqueue)
1557 return -ENOMEM;
1558
1559 if (read_tpd_led_state(asus) >= 0) {
1560 INIT_WORK(&asus->tpd_led_work, tpd_led_update);
1561
1562 asus->tpd_led.name = "asus::touchpad";
1563 asus->tpd_led.brightness_set = tpd_led_set;
1564 asus->tpd_led.brightness_get = tpd_led_get;
1565 asus->tpd_led.max_brightness = 1;
1566
1567 rv = led_classdev_register(&asus->platform_device->dev,
1568 &asus->tpd_led);
1569 if (rv)
1570 goto error;
1571 }
1572
1573 if (!kbd_led_read(asus, &led_val, NULL)) {
1574 asus->kbd_led_wk = led_val;
1575 asus->kbd_led.name = "asus::kbd_backlight";
1576 asus->kbd_led.flags = LED_BRIGHT_HW_CHANGED;
1577 asus->kbd_led.brightness_set = kbd_led_set;
1578 asus->kbd_led.brightness_get = kbd_led_get;
1579 asus->kbd_led.max_brightness = 3;
1580
1581 if (num_rgb_groups != 0)
1582 asus->kbd_led.groups = kbd_rgb_mode_groups;
1583
1584 rv = led_classdev_register(&asus->platform_device->dev,
1585 &asus->kbd_led);
1586 if (rv)
1587 goto error;
1588 }
1589
1590 if (asus_wmi_dev_is_present(asus, ASUS_WMI_DEVID_WIRELESS_LED)
1591 && (asus->driver->quirks->wapf > 0)) {
1592 INIT_WORK(&asus->wlan_led_work, wlan_led_update);
1593
1594 asus->wlan_led.name = "asus::wlan";
1595 asus->wlan_led.brightness_set = wlan_led_set;
1596 if (!wlan_led_unknown_state(asus))
1597 asus->wlan_led.brightness_get = wlan_led_get;
1598 asus->wlan_led.flags = LED_CORE_SUSPENDRESUME;
1599 asus->wlan_led.max_brightness = 1;
1600 asus->wlan_led.default_trigger = "asus-wlan";
1601
1602 rv = led_classdev_register(&asus->platform_device->dev,
1603 &asus->wlan_led);
1604 if (rv)
1605 goto error;
1606 }
1607
1608 if (asus_wmi_dev_is_present(asus, ASUS_WMI_DEVID_LIGHTBAR)) {
1609 INIT_WORK(&asus->lightbar_led_work, lightbar_led_update);
1610
1611 asus->lightbar_led.name = "asus::lightbar";
1612 asus->lightbar_led.brightness_set = lightbar_led_set;
1613 asus->lightbar_led.brightness_get = lightbar_led_get;
1614 asus->lightbar_led.max_brightness = 1;
1615
1616 rv = led_classdev_register(&asus->platform_device->dev,
1617 &asus->lightbar_led);
1618 }
1619
1620 if (asus_wmi_dev_is_present(asus, ASUS_WMI_DEVID_MICMUTE_LED)) {
1621 asus->micmute_led.name = "platform::micmute";
1622 asus->micmute_led.max_brightness = 1;
1623 asus->micmute_led.brightness = ledtrig_audio_get(LED_AUDIO_MICMUTE);
1624 asus->micmute_led.brightness_set_blocking = micmute_led_set;
1625 asus->micmute_led.default_trigger = "audio-micmute";
1626
1627 rv = led_classdev_register(&asus->platform_device->dev,
1628 &asus->micmute_led);
1629 if (rv)
1630 goto error;
1631 }
1632
1633error:
1634 if (rv)
1635 asus_wmi_led_exit(asus);
1636
1637 return rv;
1638}
1639
1640/* RF *************************************************************************/
1641
1642/*
1643 * PCI hotplug (for wlan rfkill)
1644 */
1645static bool asus_wlan_rfkill_blocked(struct asus_wmi *asus)
1646{
1647 int result = asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_WLAN);
1648
1649 if (result < 0)
1650 return false;
1651 return !result;
1652}
1653
1654static void asus_rfkill_hotplug(struct asus_wmi *asus)
1655{
1656 struct pci_dev *dev;
1657 struct pci_bus *bus;
1658 bool blocked;
1659 bool absent;
1660 u32 l;
1661
1662 mutex_lock(&asus->wmi_lock);
1663 blocked = asus_wlan_rfkill_blocked(asus);
1664 mutex_unlock(&asus->wmi_lock);
1665
1666 mutex_lock(&asus->hotplug_lock);
1667 pci_lock_rescan_remove();
1668
1669 if (asus->wlan.rfkill)
1670 rfkill_set_sw_state(asus->wlan.rfkill, blocked);
1671
1672 if (asus->hotplug_slot.ops) {
1673 bus = pci_find_bus(0, 1);
1674 if (!bus) {
1675 pr_warn("Unable to find PCI bus 1?\n");
1676 goto out_unlock;
1677 }
1678
1679 if (pci_bus_read_config_dword(bus, 0, PCI_VENDOR_ID, &l)) {
1680 pr_err("Unable to read PCI config space?\n");
1681 goto out_unlock;
1682 }
1683 absent = (l == 0xffffffff);
1684
1685 if (blocked != absent) {
1686 pr_warn("BIOS says wireless lan is %s, but the pci device is %s\n",
1687 blocked ? "blocked" : "unblocked",
1688 absent ? "absent" : "present");
1689 pr_warn("skipped wireless hotplug as probably inappropriate for this model\n");
1690 goto out_unlock;
1691 }
1692
1693 if (!blocked) {
1694 dev = pci_get_slot(bus, 0);
1695 if (dev) {
1696 /* Device already present */
1697 pci_dev_put(dev);
1698 goto out_unlock;
1699 }
1700 dev = pci_scan_single_device(bus, 0);
1701 if (dev) {
1702 pci_bus_assign_resources(bus);
1703 pci_bus_add_device(dev);
1704 }
1705 } else {
1706 dev = pci_get_slot(bus, 0);
1707 if (dev) {
1708 pci_stop_and_remove_bus_device(dev);
1709 pci_dev_put(dev);
1710 }
1711 }
1712 }
1713
1714out_unlock:
1715 pci_unlock_rescan_remove();
1716 mutex_unlock(&asus->hotplug_lock);
1717}
1718
1719static void asus_rfkill_notify(acpi_handle handle, u32 event, void *data)
1720{
1721 struct asus_wmi *asus = data;
1722
1723 if (event != ACPI_NOTIFY_BUS_CHECK)
1724 return;
1725
1726 /*
1727 * We can't call directly asus_rfkill_hotplug because most
1728 * of the time WMBC is still being executed and not reetrant.
1729 * There is currently no way to tell ACPICA that we want this
1730 * method to be serialized, we schedule a asus_rfkill_hotplug
1731 * call later, in a safer context.
1732 */
1733 queue_work(asus->hotplug_workqueue, &asus->hotplug_work);
1734}
1735
1736static int asus_register_rfkill_notifier(struct asus_wmi *asus, char *node)
1737{
1738 acpi_status status;
1739 acpi_handle handle;
1740
1741 status = acpi_get_handle(NULL, node, &handle);
1742 if (ACPI_FAILURE(status))
1743 return -ENODEV;
1744
1745 status = acpi_install_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
1746 asus_rfkill_notify, asus);
1747 if (ACPI_FAILURE(status))
1748 pr_warn("Failed to register notify on %s\n", node);
1749
1750 return 0;
1751}
1752
1753static void asus_unregister_rfkill_notifier(struct asus_wmi *asus, char *node)
1754{
1755 acpi_status status = AE_OK;
1756 acpi_handle handle;
1757
1758 status = acpi_get_handle(NULL, node, &handle);
1759 if (ACPI_FAILURE(status))
1760 return;
1761
1762 status = acpi_remove_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
1763 asus_rfkill_notify);
1764 if (ACPI_FAILURE(status))
1765 pr_err("Error removing rfkill notify handler %s\n", node);
1766}
1767
1768static int asus_get_adapter_status(struct hotplug_slot *hotplug_slot,
1769 u8 *value)
1770{
1771 struct asus_wmi *asus = container_of(hotplug_slot,
1772 struct asus_wmi, hotplug_slot);
1773 int result = asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_WLAN);
1774
1775 if (result < 0)
1776 return result;
1777
1778 *value = !!result;
1779 return 0;
1780}
1781
1782static const struct hotplug_slot_ops asus_hotplug_slot_ops = {
1783 .get_adapter_status = asus_get_adapter_status,
1784 .get_power_status = asus_get_adapter_status,
1785};
1786
1787static void asus_hotplug_work(struct work_struct *work)
1788{
1789 struct asus_wmi *asus;
1790
1791 asus = container_of(work, struct asus_wmi, hotplug_work);
1792 asus_rfkill_hotplug(asus);
1793}
1794
1795static int asus_setup_pci_hotplug(struct asus_wmi *asus)
1796{
1797 int ret = -ENOMEM;
1798 struct pci_bus *bus = pci_find_bus(0, 1);
1799
1800 if (!bus) {
1801 pr_err("Unable to find wifi PCI bus\n");
1802 return -ENODEV;
1803 }
1804
1805 asus->hotplug_workqueue =
1806 create_singlethread_workqueue("hotplug_workqueue");
1807 if (!asus->hotplug_workqueue)
1808 goto error_workqueue;
1809
1810 INIT_WORK(&asus->hotplug_work, asus_hotplug_work);
1811
1812 asus->hotplug_slot.ops = &asus_hotplug_slot_ops;
1813
1814 ret = pci_hp_register(&asus->hotplug_slot, bus, 0, "asus-wifi");
1815 if (ret) {
1816 pr_err("Unable to register hotplug slot - %d\n", ret);
1817 goto error_register;
1818 }
1819
1820 return 0;
1821
1822error_register:
1823 asus->hotplug_slot.ops = NULL;
1824 destroy_workqueue(asus->hotplug_workqueue);
1825error_workqueue:
1826 return ret;
1827}
1828
1829/*
1830 * Rfkill devices
1831 */
1832static int asus_rfkill_set(void *data, bool blocked)
1833{
1834 struct asus_rfkill *priv = data;
1835 u32 ctrl_param = !blocked;
1836 u32 dev_id = priv->dev_id;
1837
1838 /*
1839 * If the user bit is set, BIOS can't set and record the wlan status,
1840 * it will report the value read from id ASUS_WMI_DEVID_WLAN_LED
1841 * while we query the wlan status through WMI(ASUS_WMI_DEVID_WLAN).
1842 * So, we have to record wlan status in id ASUS_WMI_DEVID_WLAN_LED
1843 * while setting the wlan status through WMI.
1844 * This is also the behavior that windows app will do.
1845 */
1846 if ((dev_id == ASUS_WMI_DEVID_WLAN) &&
1847 priv->asus->driver->wlan_ctrl_by_user)
1848 dev_id = ASUS_WMI_DEVID_WLAN_LED;
1849
1850 return asus_wmi_set_devstate(dev_id, ctrl_param, NULL);
1851}
1852
1853static void asus_rfkill_query(struct rfkill *rfkill, void *data)
1854{
1855 struct asus_rfkill *priv = data;
1856 int result;
1857
1858 result = asus_wmi_get_devstate_simple(priv->asus, priv->dev_id);
1859
1860 if (result < 0)
1861 return;
1862
1863 rfkill_set_sw_state(priv->rfkill, !result);
1864}
1865
1866static int asus_rfkill_wlan_set(void *data, bool blocked)
1867{
1868 struct asus_rfkill *priv = data;
1869 struct asus_wmi *asus = priv->asus;
1870 int ret;
1871
1872 /*
1873 * This handler is enabled only if hotplug is enabled.
1874 * In this case, the asus_wmi_set_devstate() will
1875 * trigger a wmi notification and we need to wait
1876 * this call to finish before being able to call
1877 * any wmi method
1878 */
1879 mutex_lock(&asus->wmi_lock);
1880 ret = asus_rfkill_set(data, blocked);
1881 mutex_unlock(&asus->wmi_lock);
1882 return ret;
1883}
1884
1885static const struct rfkill_ops asus_rfkill_wlan_ops = {
1886 .set_block = asus_rfkill_wlan_set,
1887 .query = asus_rfkill_query,
1888};
1889
1890static const struct rfkill_ops asus_rfkill_ops = {
1891 .set_block = asus_rfkill_set,
1892 .query = asus_rfkill_query,
1893};
1894
1895static int asus_new_rfkill(struct asus_wmi *asus,
1896 struct asus_rfkill *arfkill,
1897 const char *name, enum rfkill_type type, int dev_id)
1898{
1899 int result = asus_wmi_get_devstate_simple(asus, dev_id);
1900 struct rfkill **rfkill = &arfkill->rfkill;
1901
1902 if (result < 0)
1903 return result;
1904
1905 arfkill->dev_id = dev_id;
1906 arfkill->asus = asus;
1907
1908 if (dev_id == ASUS_WMI_DEVID_WLAN &&
1909 asus->driver->quirks->hotplug_wireless)
1910 *rfkill = rfkill_alloc(name, &asus->platform_device->dev, type,
1911 &asus_rfkill_wlan_ops, arfkill);
1912 else
1913 *rfkill = rfkill_alloc(name, &asus->platform_device->dev, type,
1914 &asus_rfkill_ops, arfkill);
1915
1916 if (!*rfkill)
1917 return -EINVAL;
1918
1919 if ((dev_id == ASUS_WMI_DEVID_WLAN) &&
1920 (asus->driver->quirks->wapf > 0))
1921 rfkill_set_led_trigger_name(*rfkill, "asus-wlan");
1922
1923 rfkill_init_sw_state(*rfkill, !result);
1924 result = rfkill_register(*rfkill);
1925 if (result) {
1926 rfkill_destroy(*rfkill);
1927 *rfkill = NULL;
1928 return result;
1929 }
1930 return 0;
1931}
1932
1933static void asus_wmi_rfkill_exit(struct asus_wmi *asus)
1934{
1935 if (asus->driver->wlan_ctrl_by_user && ashs_present())
1936 return;
1937
1938 asus_unregister_rfkill_notifier(asus, "\\_SB.PCI0.P0P5");
1939 asus_unregister_rfkill_notifier(asus, "\\_SB.PCI0.P0P6");
1940 asus_unregister_rfkill_notifier(asus, "\\_SB.PCI0.P0P7");
1941 if (asus->wlan.rfkill) {
1942 rfkill_unregister(asus->wlan.rfkill);
1943 rfkill_destroy(asus->wlan.rfkill);
1944 asus->wlan.rfkill = NULL;
1945 }
1946 /*
1947 * Refresh pci hotplug in case the rfkill state was changed after
1948 * asus_unregister_rfkill_notifier()
1949 */
1950 asus_rfkill_hotplug(asus);
1951 if (asus->hotplug_slot.ops)
1952 pci_hp_deregister(&asus->hotplug_slot);
1953 if (asus->hotplug_workqueue)
1954 destroy_workqueue(asus->hotplug_workqueue);
1955
1956 if (asus->bluetooth.rfkill) {
1957 rfkill_unregister(asus->bluetooth.rfkill);
1958 rfkill_destroy(asus->bluetooth.rfkill);
1959 asus->bluetooth.rfkill = NULL;
1960 }
1961 if (asus->wimax.rfkill) {
1962 rfkill_unregister(asus->wimax.rfkill);
1963 rfkill_destroy(asus->wimax.rfkill);
1964 asus->wimax.rfkill = NULL;
1965 }
1966 if (asus->wwan3g.rfkill) {
1967 rfkill_unregister(asus->wwan3g.rfkill);
1968 rfkill_destroy(asus->wwan3g.rfkill);
1969 asus->wwan3g.rfkill = NULL;
1970 }
1971 if (asus->gps.rfkill) {
1972 rfkill_unregister(asus->gps.rfkill);
1973 rfkill_destroy(asus->gps.rfkill);
1974 asus->gps.rfkill = NULL;
1975 }
1976 if (asus->uwb.rfkill) {
1977 rfkill_unregister(asus->uwb.rfkill);
1978 rfkill_destroy(asus->uwb.rfkill);
1979 asus->uwb.rfkill = NULL;
1980 }
1981}
1982
1983static int asus_wmi_rfkill_init(struct asus_wmi *asus)
1984{
1985 int result = 0;
1986
1987 mutex_init(&asus->hotplug_lock);
1988 mutex_init(&asus->wmi_lock);
1989
1990 result = asus_new_rfkill(asus, &asus->wlan, "asus-wlan",
1991 RFKILL_TYPE_WLAN, ASUS_WMI_DEVID_WLAN);
1992
1993 if (result && result != -ENODEV)
1994 goto exit;
1995
1996 result = asus_new_rfkill(asus, &asus->bluetooth,
1997 "asus-bluetooth", RFKILL_TYPE_BLUETOOTH,
1998 ASUS_WMI_DEVID_BLUETOOTH);
1999
2000 if (result && result != -ENODEV)
2001 goto exit;
2002
2003 result = asus_new_rfkill(asus, &asus->wimax, "asus-wimax",
2004 RFKILL_TYPE_WIMAX, ASUS_WMI_DEVID_WIMAX);
2005
2006 if (result && result != -ENODEV)
2007 goto exit;
2008
2009 result = asus_new_rfkill(asus, &asus->wwan3g, "asus-wwan3g",
2010 RFKILL_TYPE_WWAN, ASUS_WMI_DEVID_WWAN3G);
2011
2012 if (result && result != -ENODEV)
2013 goto exit;
2014
2015 result = asus_new_rfkill(asus, &asus->gps, "asus-gps",
2016 RFKILL_TYPE_GPS, ASUS_WMI_DEVID_GPS);
2017
2018 if (result && result != -ENODEV)
2019 goto exit;
2020
2021 result = asus_new_rfkill(asus, &asus->uwb, "asus-uwb",
2022 RFKILL_TYPE_UWB, ASUS_WMI_DEVID_UWB);
2023
2024 if (result && result != -ENODEV)
2025 goto exit;
2026
2027 if (!asus->driver->quirks->hotplug_wireless)
2028 goto exit;
2029
2030 result = asus_setup_pci_hotplug(asus);
2031 /*
2032 * If we get -EBUSY then something else is handling the PCI hotplug -
2033 * don't fail in this case
2034 */
2035 if (result == -EBUSY)
2036 result = 0;
2037
2038 asus_register_rfkill_notifier(asus, "\\_SB.PCI0.P0P5");
2039 asus_register_rfkill_notifier(asus, "\\_SB.PCI0.P0P6");
2040 asus_register_rfkill_notifier(asus, "\\_SB.PCI0.P0P7");
2041 /*
2042 * Refresh pci hotplug in case the rfkill state was changed during
2043 * setup.
2044 */
2045 asus_rfkill_hotplug(asus);
2046
2047exit:
2048 if (result && result != -ENODEV)
2049 asus_wmi_rfkill_exit(asus);
2050
2051 if (result == -ENODEV)
2052 result = 0;
2053
2054 return result;
2055}
2056
2057/* Panel Overdrive ************************************************************/
2058static ssize_t panel_od_show(struct device *dev,
2059 struct device_attribute *attr, char *buf)
2060{
2061 struct asus_wmi *asus = dev_get_drvdata(dev);
2062 int result;
2063
2064 result = asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_PANEL_OD);
2065 if (result < 0)
2066 return result;
2067
2068 return sysfs_emit(buf, "%d\n", result);
2069}
2070
2071static ssize_t panel_od_store(struct device *dev,
2072 struct device_attribute *attr,
2073 const char *buf, size_t count)
2074{
2075 int result, err;
2076 u32 overdrive;
2077
2078 struct asus_wmi *asus = dev_get_drvdata(dev);
2079
2080 result = kstrtou32(buf, 10, &overdrive);
2081 if (result)
2082 return result;
2083
2084 if (overdrive > 1)
2085 return -EINVAL;
2086
2087 err = asus_wmi_set_devstate(ASUS_WMI_DEVID_PANEL_OD, overdrive, &result);
2088
2089 if (err) {
2090 pr_warn("Failed to set panel overdrive: %d\n", err);
2091 return err;
2092 }
2093
2094 if (result > 1) {
2095 pr_warn("Failed to set panel overdrive (result): 0x%x\n", result);
2096 return -EIO;
2097 }
2098
2099 sysfs_notify(&asus->platform_device->dev.kobj, NULL, "panel_od");
2100
2101 return count;
2102}
2103static DEVICE_ATTR_RW(panel_od);
2104
2105/* Mini-LED mode **************************************************************/
2106static ssize_t mini_led_mode_show(struct device *dev,
2107 struct device_attribute *attr, char *buf)
2108{
2109 struct asus_wmi *asus = dev_get_drvdata(dev);
2110 int result;
2111
2112 result = asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_MINI_LED_MODE);
2113 if (result < 0)
2114 return result;
2115
2116 return sysfs_emit(buf, "%d\n", result);
2117}
2118
2119static ssize_t mini_led_mode_store(struct device *dev,
2120 struct device_attribute *attr,
2121 const char *buf, size_t count)
2122{
2123 int result, err;
2124 u32 mode;
2125
2126 struct asus_wmi *asus = dev_get_drvdata(dev);
2127
2128 result = kstrtou32(buf, 10, &mode);
2129 if (result)
2130 return result;
2131
2132 if (mode > 1)
2133 return -EINVAL;
2134
2135 err = asus_wmi_set_devstate(ASUS_WMI_DEVID_MINI_LED_MODE, mode, &result);
2136
2137 if (err) {
2138 pr_warn("Failed to set mini-LED: %d\n", err);
2139 return err;
2140 }
2141
2142 if (result > 1) {
2143 pr_warn("Failed to set mini-LED mode (result): 0x%x\n", result);
2144 return -EIO;
2145 }
2146
2147 sysfs_notify(&asus->platform_device->dev.kobj, NULL, "mini_led_mode");
2148
2149 return count;
2150}
2151static DEVICE_ATTR_RW(mini_led_mode);
2152
2153/* Quirks *********************************************************************/
2154
2155static void asus_wmi_set_xusb2pr(struct asus_wmi *asus)
2156{
2157 struct pci_dev *xhci_pdev;
2158 u32 orig_ports_available;
2159 u32 ports_available = asus->driver->quirks->xusb2pr;
2160
2161 xhci_pdev = pci_get_device(PCI_VENDOR_ID_INTEL,
2162 PCI_DEVICE_ID_INTEL_LYNXPOINT_LP_XHCI,
2163 NULL);
2164
2165 if (!xhci_pdev)
2166 return;
2167
2168 pci_read_config_dword(xhci_pdev, USB_INTEL_XUSB2PR,
2169 &orig_ports_available);
2170
2171 pci_write_config_dword(xhci_pdev, USB_INTEL_XUSB2PR,
2172 cpu_to_le32(ports_available));
2173
2174 pci_dev_put(xhci_pdev);
2175
2176 pr_info("set USB_INTEL_XUSB2PR old: 0x%04x, new: 0x%04x\n",
2177 orig_ports_available, ports_available);
2178}
2179
2180/*
2181 * Some devices dont support or have borcken get_als method
2182 * but still support set method.
2183 */
2184static void asus_wmi_set_als(void)
2185{
2186 asus_wmi_set_devstate(ASUS_WMI_DEVID_ALS_ENABLE, 1, NULL);
2187}
2188
2189/* Hwmon device ***************************************************************/
2190
2191static int asus_agfn_fan_speed_read(struct asus_wmi *asus, int fan,
2192 int *speed)
2193{
2194 struct agfn_fan_args args = {
2195 .agfn.len = sizeof(args),
2196 .agfn.mfun = ASUS_FAN_MFUN,
2197 .agfn.sfun = ASUS_FAN_SFUN_READ,
2198 .fan = fan,
2199 .speed = 0,
2200 };
2201 struct acpi_buffer input = { (acpi_size) sizeof(args), &args };
2202 int status;
2203
2204 if (fan != 1)
2205 return -EINVAL;
2206
2207 status = asus_wmi_evaluate_method_agfn(input);
2208
2209 if (status || args.agfn.err)
2210 return -ENXIO;
2211
2212 if (speed)
2213 *speed = args.speed;
2214
2215 return 0;
2216}
2217
2218static int asus_agfn_fan_speed_write(struct asus_wmi *asus, int fan,
2219 int *speed)
2220{
2221 struct agfn_fan_args args = {
2222 .agfn.len = sizeof(args),
2223 .agfn.mfun = ASUS_FAN_MFUN,
2224 .agfn.sfun = ASUS_FAN_SFUN_WRITE,
2225 .fan = fan,
2226 .speed = speed ? *speed : 0,
2227 };
2228 struct acpi_buffer input = { (acpi_size) sizeof(args), &args };
2229 int status;
2230
2231 /* 1: for setting 1st fan's speed 0: setting auto mode */
2232 if (fan != 1 && fan != 0)
2233 return -EINVAL;
2234
2235 status = asus_wmi_evaluate_method_agfn(input);
2236
2237 if (status || args.agfn.err)
2238 return -ENXIO;
2239
2240 if (speed && fan == 1)
2241 asus->agfn_pwm = *speed;
2242
2243 return 0;
2244}
2245
2246/*
2247 * Check if we can read the speed of one fan. If true we assume we can also
2248 * control it.
2249 */
2250static bool asus_wmi_has_agfn_fan(struct asus_wmi *asus)
2251{
2252 int status;
2253 int speed;
2254 u32 value;
2255
2256 status = asus_agfn_fan_speed_read(asus, 1, &speed);
2257 if (status != 0)
2258 return false;
2259
2260 status = asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_FAN_CTRL, &value);
2261 if (status != 0)
2262 return false;
2263
2264 /*
2265 * We need to find a better way, probably using sfun,
2266 * bits or spec ...
2267 * Currently we disable it if:
2268 * - ASUS_WMI_UNSUPPORTED_METHOD is returned
2269 * - reverved bits are non-zero
2270 * - sfun and presence bit are not set
2271 */
2272 return !(value == ASUS_WMI_UNSUPPORTED_METHOD || value & 0xFFF80000
2273 || (!asus->sfun && !(value & ASUS_WMI_DSTS_PRESENCE_BIT)));
2274}
2275
2276static int asus_fan_set_auto(struct asus_wmi *asus)
2277{
2278 int status;
2279 u32 retval;
2280
2281 switch (asus->fan_type) {
2282 case FAN_TYPE_SPEC83:
2283 status = asus_wmi_set_devstate(ASUS_WMI_DEVID_CPU_FAN_CTRL,
2284 0, &retval);
2285 if (status)
2286 return status;
2287
2288 if (retval != 1)
2289 return -EIO;
2290 break;
2291
2292 case FAN_TYPE_AGFN:
2293 status = asus_agfn_fan_speed_write(asus, 0, NULL);
2294 if (status)
2295 return -ENXIO;
2296 break;
2297
2298 default:
2299 return -ENXIO;
2300 }
2301
2302 /*
2303 * Modern models like the G713 also have GPU fan control (this is not AGFN)
2304 */
2305 if (asus->gpu_fan_type == FAN_TYPE_SPEC83) {
2306 status = asus_wmi_set_devstate(ASUS_WMI_DEVID_GPU_FAN_CTRL,
2307 0, &retval);
2308 if (status)
2309 return status;
2310
2311 if (retval != 1)
2312 return -EIO;
2313 }
2314
2315 return 0;
2316}
2317
2318static ssize_t pwm1_show(struct device *dev,
2319 struct device_attribute *attr,
2320 char *buf)
2321{
2322 struct asus_wmi *asus = dev_get_drvdata(dev);
2323 int err;
2324 int value;
2325
2326 /* If we already set a value then just return it */
2327 if (asus->agfn_pwm >= 0)
2328 return sprintf(buf, "%d\n", asus->agfn_pwm);
2329
2330 /*
2331 * If we haven't set already set a value through the AGFN interface,
2332 * we read a current value through the (now-deprecated) FAN_CTRL device.
2333 */
2334 err = asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_FAN_CTRL, &value);
2335 if (err < 0)
2336 return err;
2337
2338 value &= 0xFF;
2339
2340 if (value == 1) /* Low Speed */
2341 value = 85;
2342 else if (value == 2)
2343 value = 170;
2344 else if (value == 3)
2345 value = 255;
2346 else if (value) {
2347 pr_err("Unknown fan speed %#x\n", value);
2348 value = -1;
2349 }
2350
2351 return sysfs_emit(buf, "%d\n", value);
2352}
2353
2354static ssize_t pwm1_store(struct device *dev,
2355 struct device_attribute *attr,
2356 const char *buf, size_t count) {
2357 struct asus_wmi *asus = dev_get_drvdata(dev);
2358 int value;
2359 int state;
2360 int ret;
2361
2362 ret = kstrtouint(buf, 10, &value);
2363 if (ret)
2364 return ret;
2365
2366 value = clamp(value, 0, 255);
2367
2368 state = asus_agfn_fan_speed_write(asus, 1, &value);
2369 if (state)
2370 pr_warn("Setting fan speed failed: %d\n", state);
2371 else
2372 asus->fan_pwm_mode = ASUS_FAN_CTRL_MANUAL;
2373
2374 return count;
2375}
2376
2377static ssize_t fan1_input_show(struct device *dev,
2378 struct device_attribute *attr,
2379 char *buf)
2380{
2381 struct asus_wmi *asus = dev_get_drvdata(dev);
2382 int value;
2383 int ret;
2384
2385 switch (asus->fan_type) {
2386 case FAN_TYPE_SPEC83:
2387 ret = asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_CPU_FAN_CTRL,
2388 &value);
2389 if (ret < 0)
2390 return ret;
2391
2392 value &= 0xffff;
2393 break;
2394
2395 case FAN_TYPE_AGFN:
2396 /* no speed readable on manual mode */
2397 if (asus->fan_pwm_mode == ASUS_FAN_CTRL_MANUAL)
2398 return -ENXIO;
2399
2400 ret = asus_agfn_fan_speed_read(asus, 1, &value);
2401 if (ret) {
2402 pr_warn("reading fan speed failed: %d\n", ret);
2403 return -ENXIO;
2404 }
2405 break;
2406
2407 default:
2408 return -ENXIO;
2409 }
2410
2411 return sysfs_emit(buf, "%d\n", value < 0 ? -1 : value * 100);
2412}
2413
2414static ssize_t pwm1_enable_show(struct device *dev,
2415 struct device_attribute *attr,
2416 char *buf)
2417{
2418 struct asus_wmi *asus = dev_get_drvdata(dev);
2419
2420 /*
2421 * Just read back the cached pwm mode.
2422 *
2423 * For the CPU_FAN device, the spec indicates that we should be
2424 * able to read the device status and consult bit 19 to see if we
2425 * are in Full On or Automatic mode. However, this does not work
2426 * in practice on X532FL at least (the bit is always 0) and there's
2427 * also nothing in the DSDT to indicate that this behaviour exists.
2428 */
2429 return sysfs_emit(buf, "%d\n", asus->fan_pwm_mode);
2430}
2431
2432static ssize_t pwm1_enable_store(struct device *dev,
2433 struct device_attribute *attr,
2434 const char *buf, size_t count)
2435{
2436 struct asus_wmi *asus = dev_get_drvdata(dev);
2437 int status = 0;
2438 int state;
2439 int value;
2440 int ret;
2441 u32 retval;
2442
2443 ret = kstrtouint(buf, 10, &state);
2444 if (ret)
2445 return ret;
2446
2447 if (asus->fan_type == FAN_TYPE_SPEC83) {
2448 switch (state) { /* standard documented hwmon values */
2449 case ASUS_FAN_CTRL_FULLSPEED:
2450 value = 1;
2451 break;
2452 case ASUS_FAN_CTRL_AUTO:
2453 value = 0;
2454 break;
2455 default:
2456 return -EINVAL;
2457 }
2458
2459 ret = asus_wmi_set_devstate(ASUS_WMI_DEVID_CPU_FAN_CTRL,
2460 value, &retval);
2461 if (ret)
2462 return ret;
2463
2464 if (retval != 1)
2465 return -EIO;
2466 } else if (asus->fan_type == FAN_TYPE_AGFN) {
2467 switch (state) {
2468 case ASUS_FAN_CTRL_MANUAL:
2469 break;
2470
2471 case ASUS_FAN_CTRL_AUTO:
2472 status = asus_fan_set_auto(asus);
2473 if (status)
2474 return status;
2475 break;
2476
2477 default:
2478 return -EINVAL;
2479 }
2480 }
2481
2482 asus->fan_pwm_mode = state;
2483
2484 /* Must set to disabled if mode is toggled */
2485 if (asus->cpu_fan_curve_available)
2486 asus->custom_fan_curves[FAN_CURVE_DEV_CPU].enabled = false;
2487 if (asus->gpu_fan_curve_available)
2488 asus->custom_fan_curves[FAN_CURVE_DEV_GPU].enabled = false;
2489 if (asus->mid_fan_curve_available)
2490 asus->custom_fan_curves[FAN_CURVE_DEV_MID].enabled = false;
2491
2492 return count;
2493}
2494
2495static ssize_t fan1_label_show(struct device *dev,
2496 struct device_attribute *attr,
2497 char *buf)
2498{
2499 return sysfs_emit(buf, "%s\n", ASUS_FAN_DESC);
2500}
2501
2502static ssize_t asus_hwmon_temp1(struct device *dev,
2503 struct device_attribute *attr,
2504 char *buf)
2505{
2506 struct asus_wmi *asus = dev_get_drvdata(dev);
2507 u32 value;
2508 int err;
2509
2510 err = asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_THERMAL_CTRL, &value);
2511 if (err < 0)
2512 return err;
2513
2514 return sprintf(buf, "%ld\n",
2515 deci_kelvin_to_millicelsius(value & 0xFFFF));
2516}
2517
2518/* GPU fan on modern ROG laptops */
2519static ssize_t fan2_input_show(struct device *dev,
2520 struct device_attribute *attr,
2521 char *buf)
2522{
2523 struct asus_wmi *asus = dev_get_drvdata(dev);
2524 int value;
2525 int ret;
2526
2527 ret = asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_GPU_FAN_CTRL, &value);
2528 if (ret < 0)
2529 return ret;
2530
2531 value &= 0xffff;
2532
2533 return sysfs_emit(buf, "%d\n", value * 100);
2534}
2535
2536static ssize_t fan2_label_show(struct device *dev,
2537 struct device_attribute *attr,
2538 char *buf)
2539{
2540 return sysfs_emit(buf, "%s\n", ASUS_GPU_FAN_DESC);
2541}
2542
2543/* Middle/Center fan on modern ROG laptops */
2544static ssize_t fan3_input_show(struct device *dev,
2545 struct device_attribute *attr,
2546 char *buf)
2547{
2548 struct asus_wmi *asus = dev_get_drvdata(dev);
2549 int value;
2550 int ret;
2551
2552 ret = asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_MID_FAN_CTRL, &value);
2553 if (ret < 0)
2554 return ret;
2555
2556 value &= 0xffff;
2557
2558 return sysfs_emit(buf, "%d\n", value * 100);
2559}
2560
2561static ssize_t fan3_label_show(struct device *dev,
2562 struct device_attribute *attr,
2563 char *buf)
2564{
2565 return sysfs_emit(buf, "%s\n", ASUS_MID_FAN_DESC);
2566}
2567
2568static ssize_t pwm2_enable_show(struct device *dev,
2569 struct device_attribute *attr,
2570 char *buf)
2571{
2572 struct asus_wmi *asus = dev_get_drvdata(dev);
2573
2574 return sysfs_emit(buf, "%d\n", asus->gpu_fan_pwm_mode);
2575}
2576
2577static ssize_t pwm2_enable_store(struct device *dev,
2578 struct device_attribute *attr,
2579 const char *buf, size_t count)
2580{
2581 struct asus_wmi *asus = dev_get_drvdata(dev);
2582 int state;
2583 int value;
2584 int ret;
2585 u32 retval;
2586
2587 ret = kstrtouint(buf, 10, &state);
2588 if (ret)
2589 return ret;
2590
2591 switch (state) { /* standard documented hwmon values */
2592 case ASUS_FAN_CTRL_FULLSPEED:
2593 value = 1;
2594 break;
2595 case ASUS_FAN_CTRL_AUTO:
2596 value = 0;
2597 break;
2598 default:
2599 return -EINVAL;
2600 }
2601
2602 ret = asus_wmi_set_devstate(ASUS_WMI_DEVID_GPU_FAN_CTRL,
2603 value, &retval);
2604 if (ret)
2605 return ret;
2606
2607 if (retval != 1)
2608 return -EIO;
2609
2610 asus->gpu_fan_pwm_mode = state;
2611 return count;
2612}
2613
2614static ssize_t pwm3_enable_show(struct device *dev,
2615 struct device_attribute *attr,
2616 char *buf)
2617{
2618 struct asus_wmi *asus = dev_get_drvdata(dev);
2619
2620 return sysfs_emit(buf, "%d\n", asus->mid_fan_pwm_mode);
2621}
2622
2623static ssize_t pwm3_enable_store(struct device *dev,
2624 struct device_attribute *attr,
2625 const char *buf, size_t count)
2626{
2627 struct asus_wmi *asus = dev_get_drvdata(dev);
2628 int state;
2629 int value;
2630 int ret;
2631 u32 retval;
2632
2633 ret = kstrtouint(buf, 10, &state);
2634 if (ret)
2635 return ret;
2636
2637 switch (state) { /* standard documented hwmon values */
2638 case ASUS_FAN_CTRL_FULLSPEED:
2639 value = 1;
2640 break;
2641 case ASUS_FAN_CTRL_AUTO:
2642 value = 0;
2643 break;
2644 default:
2645 return -EINVAL;
2646 }
2647
2648 ret = asus_wmi_set_devstate(ASUS_WMI_DEVID_MID_FAN_CTRL,
2649 value, &retval);
2650 if (ret)
2651 return ret;
2652
2653 if (retval != 1)
2654 return -EIO;
2655
2656 asus->mid_fan_pwm_mode = state;
2657 return count;
2658}
2659
2660/* Fan1 */
2661static DEVICE_ATTR_RW(pwm1);
2662static DEVICE_ATTR_RW(pwm1_enable);
2663static DEVICE_ATTR_RO(fan1_input);
2664static DEVICE_ATTR_RO(fan1_label);
2665/* Fan2 - GPU fan */
2666static DEVICE_ATTR_RW(pwm2_enable);
2667static DEVICE_ATTR_RO(fan2_input);
2668static DEVICE_ATTR_RO(fan2_label);
2669/* Fan3 - Middle/center fan */
2670static DEVICE_ATTR_RW(pwm3_enable);
2671static DEVICE_ATTR_RO(fan3_input);
2672static DEVICE_ATTR_RO(fan3_label);
2673
2674/* Temperature */
2675static DEVICE_ATTR(temp1_input, S_IRUGO, asus_hwmon_temp1, NULL);
2676
2677static struct attribute *hwmon_attributes[] = {
2678 &dev_attr_pwm1.attr,
2679 &dev_attr_pwm1_enable.attr,
2680 &dev_attr_pwm2_enable.attr,
2681 &dev_attr_pwm3_enable.attr,
2682 &dev_attr_fan1_input.attr,
2683 &dev_attr_fan1_label.attr,
2684 &dev_attr_fan2_input.attr,
2685 &dev_attr_fan2_label.attr,
2686 &dev_attr_fan3_input.attr,
2687 &dev_attr_fan3_label.attr,
2688
2689 &dev_attr_temp1_input.attr,
2690 NULL
2691};
2692
2693static umode_t asus_hwmon_sysfs_is_visible(struct kobject *kobj,
2694 struct attribute *attr, int idx)
2695{
2696 struct device *dev = kobj_to_dev(kobj);
2697 struct asus_wmi *asus = dev_get_drvdata(dev->parent);
2698 u32 value = ASUS_WMI_UNSUPPORTED_METHOD;
2699
2700 if (attr == &dev_attr_pwm1.attr) {
2701 if (asus->fan_type != FAN_TYPE_AGFN)
2702 return 0;
2703 } else if (attr == &dev_attr_fan1_input.attr
2704 || attr == &dev_attr_fan1_label.attr
2705 || attr == &dev_attr_pwm1_enable.attr) {
2706 if (asus->fan_type == FAN_TYPE_NONE)
2707 return 0;
2708 } else if (attr == &dev_attr_fan2_input.attr
2709 || attr == &dev_attr_fan2_label.attr
2710 || attr == &dev_attr_pwm2_enable.attr) {
2711 if (asus->gpu_fan_type == FAN_TYPE_NONE)
2712 return 0;
2713 } else if (attr == &dev_attr_fan3_input.attr
2714 || attr == &dev_attr_fan3_label.attr
2715 || attr == &dev_attr_pwm3_enable.attr) {
2716 if (asus->mid_fan_type == FAN_TYPE_NONE)
2717 return 0;
2718 } else if (attr == &dev_attr_temp1_input.attr) {
2719 int err = asus_wmi_get_devstate(asus,
2720 ASUS_WMI_DEVID_THERMAL_CTRL,
2721 &value);
2722
2723 if (err < 0)
2724 return 0; /* can't return negative here */
2725
2726 /*
2727 * If the temperature value in deci-Kelvin is near the absolute
2728 * zero temperature, something is clearly wrong
2729 */
2730 if (value == 0 || value == 1)
2731 return 0;
2732 }
2733
2734 return attr->mode;
2735}
2736
2737static const struct attribute_group hwmon_attribute_group = {
2738 .is_visible = asus_hwmon_sysfs_is_visible,
2739 .attrs = hwmon_attributes
2740};
2741__ATTRIBUTE_GROUPS(hwmon_attribute);
2742
2743static int asus_wmi_hwmon_init(struct asus_wmi *asus)
2744{
2745 struct device *dev = &asus->platform_device->dev;
2746 struct device *hwmon;
2747
2748 hwmon = devm_hwmon_device_register_with_groups(dev, "asus", asus,
2749 hwmon_attribute_groups);
2750
2751 if (IS_ERR(hwmon)) {
2752 pr_err("Could not register asus hwmon device\n");
2753 return PTR_ERR(hwmon);
2754 }
2755 return 0;
2756}
2757
2758static int asus_wmi_fan_init(struct asus_wmi *asus)
2759{
2760 asus->gpu_fan_type = FAN_TYPE_NONE;
2761 asus->mid_fan_type = FAN_TYPE_NONE;
2762 asus->fan_type = FAN_TYPE_NONE;
2763 asus->agfn_pwm = -1;
2764
2765 if (asus->driver->quirks->wmi_ignore_fan)
2766 asus->fan_type = FAN_TYPE_NONE;
2767 else if (asus_wmi_dev_is_present(asus, ASUS_WMI_DEVID_CPU_FAN_CTRL))
2768 asus->fan_type = FAN_TYPE_SPEC83;
2769 else if (asus_wmi_has_agfn_fan(asus))
2770 asus->fan_type = FAN_TYPE_AGFN;
2771
2772 /* Modern models like G713 also have GPU fan control */
2773 if (asus_wmi_dev_is_present(asus, ASUS_WMI_DEVID_GPU_FAN_CTRL))
2774 asus->gpu_fan_type = FAN_TYPE_SPEC83;
2775
2776 /* Some models also have a center/middle fan */
2777 if (asus_wmi_dev_is_present(asus, ASUS_WMI_DEVID_MID_FAN_CTRL))
2778 asus->mid_fan_type = FAN_TYPE_SPEC83;
2779
2780 if (asus->fan_type == FAN_TYPE_NONE)
2781 return -ENODEV;
2782
2783 asus_fan_set_auto(asus);
2784 asus->fan_pwm_mode = ASUS_FAN_CTRL_AUTO;
2785 return 0;
2786}
2787
2788/* Fan mode *******************************************************************/
2789
2790static int fan_boost_mode_check_present(struct asus_wmi *asus)
2791{
2792 u32 result;
2793 int err;
2794
2795 asus->fan_boost_mode_available = false;
2796
2797 err = asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_FAN_BOOST_MODE,
2798 &result);
2799 if (err) {
2800 if (err == -ENODEV)
2801 return 0;
2802 else
2803 return err;
2804 }
2805
2806 if ((result & ASUS_WMI_DSTS_PRESENCE_BIT) &&
2807 (result & ASUS_FAN_BOOST_MODES_MASK)) {
2808 asus->fan_boost_mode_available = true;
2809 asus->fan_boost_mode_mask = result & ASUS_FAN_BOOST_MODES_MASK;
2810 }
2811
2812 return 0;
2813}
2814
2815static int fan_boost_mode_write(struct asus_wmi *asus)
2816{
2817 u32 retval;
2818 u8 value;
2819 int err;
2820
2821 value = asus->fan_boost_mode;
2822
2823 pr_info("Set fan boost mode: %u\n", value);
2824 err = asus_wmi_set_devstate(ASUS_WMI_DEVID_FAN_BOOST_MODE, value,
2825 &retval);
2826
2827 sysfs_notify(&asus->platform_device->dev.kobj, NULL,
2828 "fan_boost_mode");
2829
2830 if (err) {
2831 pr_warn("Failed to set fan boost mode: %d\n", err);
2832 return err;
2833 }
2834
2835 if (retval != 1) {
2836 pr_warn("Failed to set fan boost mode (retval): 0x%x\n",
2837 retval);
2838 return -EIO;
2839 }
2840
2841 return 0;
2842}
2843
2844static int fan_boost_mode_switch_next(struct asus_wmi *asus)
2845{
2846 u8 mask = asus->fan_boost_mode_mask;
2847
2848 if (asus->fan_boost_mode == ASUS_FAN_BOOST_MODE_NORMAL) {
2849 if (mask & ASUS_FAN_BOOST_MODE_OVERBOOST_MASK)
2850 asus->fan_boost_mode = ASUS_FAN_BOOST_MODE_OVERBOOST;
2851 else if (mask & ASUS_FAN_BOOST_MODE_SILENT_MASK)
2852 asus->fan_boost_mode = ASUS_FAN_BOOST_MODE_SILENT;
2853 } else if (asus->fan_boost_mode == ASUS_FAN_BOOST_MODE_OVERBOOST) {
2854 if (mask & ASUS_FAN_BOOST_MODE_SILENT_MASK)
2855 asus->fan_boost_mode = ASUS_FAN_BOOST_MODE_SILENT;
2856 else
2857 asus->fan_boost_mode = ASUS_FAN_BOOST_MODE_NORMAL;
2858 } else {
2859 asus->fan_boost_mode = ASUS_FAN_BOOST_MODE_NORMAL;
2860 }
2861
2862 return fan_boost_mode_write(asus);
2863}
2864
2865static ssize_t fan_boost_mode_show(struct device *dev,
2866 struct device_attribute *attr, char *buf)
2867{
2868 struct asus_wmi *asus = dev_get_drvdata(dev);
2869
2870 return sysfs_emit(buf, "%d\n", asus->fan_boost_mode);
2871}
2872
2873static ssize_t fan_boost_mode_store(struct device *dev,
2874 struct device_attribute *attr,
2875 const char *buf, size_t count)
2876{
2877 struct asus_wmi *asus = dev_get_drvdata(dev);
2878 u8 mask = asus->fan_boost_mode_mask;
2879 u8 new_mode;
2880 int result;
2881
2882 result = kstrtou8(buf, 10, &new_mode);
2883 if (result < 0) {
2884 pr_warn("Trying to store invalid value\n");
2885 return result;
2886 }
2887
2888 if (new_mode == ASUS_FAN_BOOST_MODE_OVERBOOST) {
2889 if (!(mask & ASUS_FAN_BOOST_MODE_OVERBOOST_MASK))
2890 return -EINVAL;
2891 } else if (new_mode == ASUS_FAN_BOOST_MODE_SILENT) {
2892 if (!(mask & ASUS_FAN_BOOST_MODE_SILENT_MASK))
2893 return -EINVAL;
2894 } else if (new_mode != ASUS_FAN_BOOST_MODE_NORMAL) {
2895 return -EINVAL;
2896 }
2897
2898 asus->fan_boost_mode = new_mode;
2899 fan_boost_mode_write(asus);
2900
2901 return count;
2902}
2903
2904// Fan boost mode: 0 - normal, 1 - overboost, 2 - silent
2905static DEVICE_ATTR_RW(fan_boost_mode);
2906
2907/* Custom fan curves **********************************************************/
2908
2909static void fan_curve_copy_from_buf(struct fan_curve_data *data, u8 *buf)
2910{
2911 int i;
2912
2913 for (i = 0; i < FAN_CURVE_POINTS; i++) {
2914 data->temps[i] = buf[i];
2915 }
2916
2917 for (i = 0; i < FAN_CURVE_POINTS; i++) {
2918 data->percents[i] =
2919 255 * buf[i + FAN_CURVE_POINTS] / 100;
2920 }
2921}
2922
2923static int fan_curve_get_factory_default(struct asus_wmi *asus, u32 fan_dev)
2924{
2925 struct fan_curve_data *curves;
2926 u8 buf[FAN_CURVE_BUF_LEN];
2927 int err, fan_idx;
2928 u8 mode = 0;
2929
2930 if (asus->throttle_thermal_policy_available)
2931 mode = asus->throttle_thermal_policy_mode;
2932 /* DEVID_<C/G>PU_FAN_CURVE is switched for OVERBOOST vs SILENT */
2933 if (mode == 2)
2934 mode = 1;
2935 else if (mode == 1)
2936 mode = 2;
2937
2938 err = asus_wmi_evaluate_method_buf(asus->dsts_id, fan_dev, mode, buf,
2939 FAN_CURVE_BUF_LEN);
2940 if (err) {
2941 pr_warn("%s (0x%08x) failed: %d\n", __func__, fan_dev, err);
2942 return err;
2943 }
2944
2945 fan_idx = FAN_CURVE_DEV_CPU;
2946 if (fan_dev == ASUS_WMI_DEVID_GPU_FAN_CURVE)
2947 fan_idx = FAN_CURVE_DEV_GPU;
2948
2949 if (fan_dev == ASUS_WMI_DEVID_MID_FAN_CURVE)
2950 fan_idx = FAN_CURVE_DEV_MID;
2951
2952 curves = &asus->custom_fan_curves[fan_idx];
2953 curves->device_id = fan_dev;
2954
2955 fan_curve_copy_from_buf(curves, buf);
2956 return 0;
2957}
2958
2959/* Check if capability exists, and populate defaults */
2960static int fan_curve_check_present(struct asus_wmi *asus, bool *available,
2961 u32 fan_dev)
2962{
2963 int err;
2964
2965 *available = false;
2966
2967 if (asus->fan_type == FAN_TYPE_NONE)
2968 return 0;
2969
2970 err = fan_curve_get_factory_default(asus, fan_dev);
2971 if (err) {
2972 return 0;
2973 }
2974
2975 *available = true;
2976 return 0;
2977}
2978
2979/* Determine which fan the attribute is for if SENSOR_ATTR */
2980static struct fan_curve_data *fan_curve_attr_select(struct asus_wmi *asus,
2981 struct device_attribute *attr)
2982{
2983 int index = to_sensor_dev_attr(attr)->index;
2984
2985 return &asus->custom_fan_curves[index];
2986}
2987
2988/* Determine which fan the attribute is for if SENSOR_ATTR_2 */
2989static struct fan_curve_data *fan_curve_attr_2_select(struct asus_wmi *asus,
2990 struct device_attribute *attr)
2991{
2992 int nr = to_sensor_dev_attr_2(attr)->nr;
2993
2994 return &asus->custom_fan_curves[nr & ~FAN_CURVE_PWM_MASK];
2995}
2996
2997static ssize_t fan_curve_show(struct device *dev,
2998 struct device_attribute *attr, char *buf)
2999{
3000 struct sensor_device_attribute_2 *dev_attr = to_sensor_dev_attr_2(attr);
3001 struct asus_wmi *asus = dev_get_drvdata(dev);
3002 struct fan_curve_data *data;
3003 int value, pwm, index;
3004
3005 data = fan_curve_attr_2_select(asus, attr);
3006 pwm = dev_attr->nr & FAN_CURVE_PWM_MASK;
3007 index = dev_attr->index;
3008
3009 if (pwm)
3010 value = data->percents[index];
3011 else
3012 value = data->temps[index];
3013
3014 return sysfs_emit(buf, "%d\n", value);
3015}
3016
3017/*
3018 * "fan_dev" is the related WMI method such as ASUS_WMI_DEVID_CPU_FAN_CURVE.
3019 */
3020static int fan_curve_write(struct asus_wmi *asus,
3021 struct fan_curve_data *data)
3022{
3023 u32 arg1 = 0, arg2 = 0, arg3 = 0, arg4 = 0;
3024 u8 *percents = data->percents;
3025 u8 *temps = data->temps;
3026 int ret, i, shift = 0;
3027
3028 if (!data->enabled)
3029 return 0;
3030
3031 for (i = 0; i < FAN_CURVE_POINTS / 2; i++) {
3032 arg1 += (temps[i]) << shift;
3033 arg2 += (temps[i + 4]) << shift;
3034 /* Scale to percentage for device */
3035 arg3 += (100 * percents[i] / 255) << shift;
3036 arg4 += (100 * percents[i + 4] / 255) << shift;
3037 shift += 8;
3038 }
3039
3040 return asus_wmi_evaluate_method5(ASUS_WMI_METHODID_DEVS,
3041 data->device_id,
3042 arg1, arg2, arg3, arg4, &ret);
3043}
3044
3045static ssize_t fan_curve_store(struct device *dev,
3046 struct device_attribute *attr, const char *buf,
3047 size_t count)
3048{
3049 struct sensor_device_attribute_2 *dev_attr = to_sensor_dev_attr_2(attr);
3050 struct asus_wmi *asus = dev_get_drvdata(dev);
3051 struct fan_curve_data *data;
3052 int err, pwm, index;
3053 u8 value;
3054
3055 data = fan_curve_attr_2_select(asus, attr);
3056 pwm = dev_attr->nr & FAN_CURVE_PWM_MASK;
3057 index = dev_attr->index;
3058
3059 err = kstrtou8(buf, 10, &value);
3060 if (err < 0)
3061 return err;
3062
3063 if (pwm)
3064 data->percents[index] = value;
3065 else
3066 data->temps[index] = value;
3067
3068 /*
3069 * Mark as disabled so the user has to explicitly enable to apply a
3070 * changed fan curve. This prevents potential lockups from writing out
3071 * many changes as one-write-per-change.
3072 */
3073 data->enabled = false;
3074
3075 return count;
3076}
3077
3078static ssize_t fan_curve_enable_show(struct device *dev,
3079 struct device_attribute *attr, char *buf)
3080{
3081 struct asus_wmi *asus = dev_get_drvdata(dev);
3082 struct fan_curve_data *data;
3083 int out = 2;
3084
3085 data = fan_curve_attr_select(asus, attr);
3086
3087 if (data->enabled)
3088 out = 1;
3089
3090 return sysfs_emit(buf, "%d\n", out);
3091}
3092
3093static ssize_t fan_curve_enable_store(struct device *dev,
3094 struct device_attribute *attr,
3095 const char *buf, size_t count)
3096{
3097 struct asus_wmi *asus = dev_get_drvdata(dev);
3098 struct fan_curve_data *data;
3099 int value, err;
3100
3101 data = fan_curve_attr_select(asus, attr);
3102
3103 err = kstrtoint(buf, 10, &value);
3104 if (err < 0)
3105 return err;
3106
3107 switch (value) {
3108 case 1:
3109 data->enabled = true;
3110 break;
3111 case 2:
3112 data->enabled = false;
3113 break;
3114 /*
3115 * Auto + reset the fan curve data to defaults. Make it an explicit
3116 * option so that users don't accidentally overwrite a set fan curve.
3117 */
3118 case 3:
3119 err = fan_curve_get_factory_default(asus, data->device_id);
3120 if (err)
3121 return err;
3122 data->enabled = false;
3123 break;
3124 default:
3125 return -EINVAL;
3126 }
3127
3128 if (data->enabled) {
3129 err = fan_curve_write(asus, data);
3130 if (err)
3131 return err;
3132 } else {
3133 /*
3134 * For machines with throttle this is the only way to reset fans
3135 * to default mode of operation (does not erase curve data).
3136 */
3137 if (asus->throttle_thermal_policy_available) {
3138 err = throttle_thermal_policy_write(asus);
3139 if (err)
3140 return err;
3141 /* Similar is true for laptops with this fan */
3142 } else if (asus->fan_type == FAN_TYPE_SPEC83) {
3143 err = asus_fan_set_auto(asus);
3144 if (err)
3145 return err;
3146 } else {
3147 /* Safeguard against fautly ACPI tables */
3148 err = fan_curve_get_factory_default(asus, data->device_id);
3149 if (err)
3150 return err;
3151 err = fan_curve_write(asus, data);
3152 if (err)
3153 return err;
3154 }
3155 }
3156 return count;
3157}
3158
3159/* CPU */
3160static SENSOR_DEVICE_ATTR_RW(pwm1_enable, fan_curve_enable, FAN_CURVE_DEV_CPU);
3161static SENSOR_DEVICE_ATTR_2_RW(pwm1_auto_point1_temp, fan_curve,
3162 FAN_CURVE_DEV_CPU, 0);
3163static SENSOR_DEVICE_ATTR_2_RW(pwm1_auto_point2_temp, fan_curve,
3164 FAN_CURVE_DEV_CPU, 1);
3165static SENSOR_DEVICE_ATTR_2_RW(pwm1_auto_point3_temp, fan_curve,
3166 FAN_CURVE_DEV_CPU, 2);
3167static SENSOR_DEVICE_ATTR_2_RW(pwm1_auto_point4_temp, fan_curve,
3168 FAN_CURVE_DEV_CPU, 3);
3169static SENSOR_DEVICE_ATTR_2_RW(pwm1_auto_point5_temp, fan_curve,
3170 FAN_CURVE_DEV_CPU, 4);
3171static SENSOR_DEVICE_ATTR_2_RW(pwm1_auto_point6_temp, fan_curve,
3172 FAN_CURVE_DEV_CPU, 5);
3173static SENSOR_DEVICE_ATTR_2_RW(pwm1_auto_point7_temp, fan_curve,
3174 FAN_CURVE_DEV_CPU, 6);
3175static SENSOR_DEVICE_ATTR_2_RW(pwm1_auto_point8_temp, fan_curve,
3176 FAN_CURVE_DEV_CPU, 7);
3177
3178static SENSOR_DEVICE_ATTR_2_RW(pwm1_auto_point1_pwm, fan_curve,
3179 FAN_CURVE_DEV_CPU | FAN_CURVE_PWM_MASK, 0);
3180static SENSOR_DEVICE_ATTR_2_RW(pwm1_auto_point2_pwm, fan_curve,
3181 FAN_CURVE_DEV_CPU | FAN_CURVE_PWM_MASK, 1);
3182static SENSOR_DEVICE_ATTR_2_RW(pwm1_auto_point3_pwm, fan_curve,
3183 FAN_CURVE_DEV_CPU | FAN_CURVE_PWM_MASK, 2);
3184static SENSOR_DEVICE_ATTR_2_RW(pwm1_auto_point4_pwm, fan_curve,
3185 FAN_CURVE_DEV_CPU | FAN_CURVE_PWM_MASK, 3);
3186static SENSOR_DEVICE_ATTR_2_RW(pwm1_auto_point5_pwm, fan_curve,
3187 FAN_CURVE_DEV_CPU | FAN_CURVE_PWM_MASK, 4);
3188static SENSOR_DEVICE_ATTR_2_RW(pwm1_auto_point6_pwm, fan_curve,
3189 FAN_CURVE_DEV_CPU | FAN_CURVE_PWM_MASK, 5);
3190static SENSOR_DEVICE_ATTR_2_RW(pwm1_auto_point7_pwm, fan_curve,
3191 FAN_CURVE_DEV_CPU | FAN_CURVE_PWM_MASK, 6);
3192static SENSOR_DEVICE_ATTR_2_RW(pwm1_auto_point8_pwm, fan_curve,
3193 FAN_CURVE_DEV_CPU | FAN_CURVE_PWM_MASK, 7);
3194
3195/* GPU */
3196static SENSOR_DEVICE_ATTR_RW(pwm2_enable, fan_curve_enable, FAN_CURVE_DEV_GPU);
3197static SENSOR_DEVICE_ATTR_2_RW(pwm2_auto_point1_temp, fan_curve,
3198 FAN_CURVE_DEV_GPU, 0);
3199static SENSOR_DEVICE_ATTR_2_RW(pwm2_auto_point2_temp, fan_curve,
3200 FAN_CURVE_DEV_GPU, 1);
3201static SENSOR_DEVICE_ATTR_2_RW(pwm2_auto_point3_temp, fan_curve,
3202 FAN_CURVE_DEV_GPU, 2);
3203static SENSOR_DEVICE_ATTR_2_RW(pwm2_auto_point4_temp, fan_curve,
3204 FAN_CURVE_DEV_GPU, 3);
3205static SENSOR_DEVICE_ATTR_2_RW(pwm2_auto_point5_temp, fan_curve,
3206 FAN_CURVE_DEV_GPU, 4);
3207static SENSOR_DEVICE_ATTR_2_RW(pwm2_auto_point6_temp, fan_curve,
3208 FAN_CURVE_DEV_GPU, 5);
3209static SENSOR_DEVICE_ATTR_2_RW(pwm2_auto_point7_temp, fan_curve,
3210 FAN_CURVE_DEV_GPU, 6);
3211static SENSOR_DEVICE_ATTR_2_RW(pwm2_auto_point8_temp, fan_curve,
3212 FAN_CURVE_DEV_GPU, 7);
3213
3214static SENSOR_DEVICE_ATTR_2_RW(pwm2_auto_point1_pwm, fan_curve,
3215 FAN_CURVE_DEV_GPU | FAN_CURVE_PWM_MASK, 0);
3216static SENSOR_DEVICE_ATTR_2_RW(pwm2_auto_point2_pwm, fan_curve,
3217 FAN_CURVE_DEV_GPU | FAN_CURVE_PWM_MASK, 1);
3218static SENSOR_DEVICE_ATTR_2_RW(pwm2_auto_point3_pwm, fan_curve,
3219 FAN_CURVE_DEV_GPU | FAN_CURVE_PWM_MASK, 2);
3220static SENSOR_DEVICE_ATTR_2_RW(pwm2_auto_point4_pwm, fan_curve,
3221 FAN_CURVE_DEV_GPU | FAN_CURVE_PWM_MASK, 3);
3222static SENSOR_DEVICE_ATTR_2_RW(pwm2_auto_point5_pwm, fan_curve,
3223 FAN_CURVE_DEV_GPU | FAN_CURVE_PWM_MASK, 4);
3224static SENSOR_DEVICE_ATTR_2_RW(pwm2_auto_point6_pwm, fan_curve,
3225 FAN_CURVE_DEV_GPU | FAN_CURVE_PWM_MASK, 5);
3226static SENSOR_DEVICE_ATTR_2_RW(pwm2_auto_point7_pwm, fan_curve,
3227 FAN_CURVE_DEV_GPU | FAN_CURVE_PWM_MASK, 6);
3228static SENSOR_DEVICE_ATTR_2_RW(pwm2_auto_point8_pwm, fan_curve,
3229 FAN_CURVE_DEV_GPU | FAN_CURVE_PWM_MASK, 7);
3230
3231/* MID */
3232static SENSOR_DEVICE_ATTR_RW(pwm3_enable, fan_curve_enable, FAN_CURVE_DEV_MID);
3233static SENSOR_DEVICE_ATTR_2_RW(pwm3_auto_point1_temp, fan_curve,
3234 FAN_CURVE_DEV_MID, 0);
3235static SENSOR_DEVICE_ATTR_2_RW(pwm3_auto_point2_temp, fan_curve,
3236 FAN_CURVE_DEV_MID, 1);
3237static SENSOR_DEVICE_ATTR_2_RW(pwm3_auto_point3_temp, fan_curve,
3238 FAN_CURVE_DEV_MID, 2);
3239static SENSOR_DEVICE_ATTR_2_RW(pwm3_auto_point4_temp, fan_curve,
3240 FAN_CURVE_DEV_MID, 3);
3241static SENSOR_DEVICE_ATTR_2_RW(pwm3_auto_point5_temp, fan_curve,
3242 FAN_CURVE_DEV_MID, 4);
3243static SENSOR_DEVICE_ATTR_2_RW(pwm3_auto_point6_temp, fan_curve,
3244 FAN_CURVE_DEV_MID, 5);
3245static SENSOR_DEVICE_ATTR_2_RW(pwm3_auto_point7_temp, fan_curve,
3246 FAN_CURVE_DEV_MID, 6);
3247static SENSOR_DEVICE_ATTR_2_RW(pwm3_auto_point8_temp, fan_curve,
3248 FAN_CURVE_DEV_MID, 7);
3249
3250static SENSOR_DEVICE_ATTR_2_RW(pwm3_auto_point1_pwm, fan_curve,
3251 FAN_CURVE_DEV_MID | FAN_CURVE_PWM_MASK, 0);
3252static SENSOR_DEVICE_ATTR_2_RW(pwm3_auto_point2_pwm, fan_curve,
3253 FAN_CURVE_DEV_MID | FAN_CURVE_PWM_MASK, 1);
3254static SENSOR_DEVICE_ATTR_2_RW(pwm3_auto_point3_pwm, fan_curve,
3255 FAN_CURVE_DEV_MID | FAN_CURVE_PWM_MASK, 2);
3256static SENSOR_DEVICE_ATTR_2_RW(pwm3_auto_point4_pwm, fan_curve,
3257 FAN_CURVE_DEV_MID | FAN_CURVE_PWM_MASK, 3);
3258static SENSOR_DEVICE_ATTR_2_RW(pwm3_auto_point5_pwm, fan_curve,
3259 FAN_CURVE_DEV_MID | FAN_CURVE_PWM_MASK, 4);
3260static SENSOR_DEVICE_ATTR_2_RW(pwm3_auto_point6_pwm, fan_curve,
3261 FAN_CURVE_DEV_MID | FAN_CURVE_PWM_MASK, 5);
3262static SENSOR_DEVICE_ATTR_2_RW(pwm3_auto_point7_pwm, fan_curve,
3263 FAN_CURVE_DEV_MID | FAN_CURVE_PWM_MASK, 6);
3264static SENSOR_DEVICE_ATTR_2_RW(pwm3_auto_point8_pwm, fan_curve,
3265 FAN_CURVE_DEV_MID | FAN_CURVE_PWM_MASK, 7);
3266
3267static struct attribute *asus_fan_curve_attr[] = {
3268 /* CPU */
3269 &sensor_dev_attr_pwm1_enable.dev_attr.attr,
3270 &sensor_dev_attr_pwm1_auto_point1_temp.dev_attr.attr,
3271 &sensor_dev_attr_pwm1_auto_point2_temp.dev_attr.attr,
3272 &sensor_dev_attr_pwm1_auto_point3_temp.dev_attr.attr,
3273 &sensor_dev_attr_pwm1_auto_point4_temp.dev_attr.attr,
3274 &sensor_dev_attr_pwm1_auto_point5_temp.dev_attr.attr,
3275 &sensor_dev_attr_pwm1_auto_point6_temp.dev_attr.attr,
3276 &sensor_dev_attr_pwm1_auto_point7_temp.dev_attr.attr,
3277 &sensor_dev_attr_pwm1_auto_point8_temp.dev_attr.attr,
3278 &sensor_dev_attr_pwm1_auto_point1_pwm.dev_attr.attr,
3279 &sensor_dev_attr_pwm1_auto_point2_pwm.dev_attr.attr,
3280 &sensor_dev_attr_pwm1_auto_point3_pwm.dev_attr.attr,
3281 &sensor_dev_attr_pwm1_auto_point4_pwm.dev_attr.attr,
3282 &sensor_dev_attr_pwm1_auto_point5_pwm.dev_attr.attr,
3283 &sensor_dev_attr_pwm1_auto_point6_pwm.dev_attr.attr,
3284 &sensor_dev_attr_pwm1_auto_point7_pwm.dev_attr.attr,
3285 &sensor_dev_attr_pwm1_auto_point8_pwm.dev_attr.attr,
3286 /* GPU */
3287 &sensor_dev_attr_pwm2_enable.dev_attr.attr,
3288 &sensor_dev_attr_pwm2_auto_point1_temp.dev_attr.attr,
3289 &sensor_dev_attr_pwm2_auto_point2_temp.dev_attr.attr,
3290 &sensor_dev_attr_pwm2_auto_point3_temp.dev_attr.attr,
3291 &sensor_dev_attr_pwm2_auto_point4_temp.dev_attr.attr,
3292 &sensor_dev_attr_pwm2_auto_point5_temp.dev_attr.attr,
3293 &sensor_dev_attr_pwm2_auto_point6_temp.dev_attr.attr,
3294 &sensor_dev_attr_pwm2_auto_point7_temp.dev_attr.attr,
3295 &sensor_dev_attr_pwm2_auto_point8_temp.dev_attr.attr,
3296 &sensor_dev_attr_pwm2_auto_point1_pwm.dev_attr.attr,
3297 &sensor_dev_attr_pwm2_auto_point2_pwm.dev_attr.attr,
3298 &sensor_dev_attr_pwm2_auto_point3_pwm.dev_attr.attr,
3299 &sensor_dev_attr_pwm2_auto_point4_pwm.dev_attr.attr,
3300 &sensor_dev_attr_pwm2_auto_point5_pwm.dev_attr.attr,
3301 &sensor_dev_attr_pwm2_auto_point6_pwm.dev_attr.attr,
3302 &sensor_dev_attr_pwm2_auto_point7_pwm.dev_attr.attr,
3303 &sensor_dev_attr_pwm2_auto_point8_pwm.dev_attr.attr,
3304 /* MID */
3305 &sensor_dev_attr_pwm3_enable.dev_attr.attr,
3306 &sensor_dev_attr_pwm3_auto_point1_temp.dev_attr.attr,
3307 &sensor_dev_attr_pwm3_auto_point2_temp.dev_attr.attr,
3308 &sensor_dev_attr_pwm3_auto_point3_temp.dev_attr.attr,
3309 &sensor_dev_attr_pwm3_auto_point4_temp.dev_attr.attr,
3310 &sensor_dev_attr_pwm3_auto_point5_temp.dev_attr.attr,
3311 &sensor_dev_attr_pwm3_auto_point6_temp.dev_attr.attr,
3312 &sensor_dev_attr_pwm3_auto_point7_temp.dev_attr.attr,
3313 &sensor_dev_attr_pwm3_auto_point8_temp.dev_attr.attr,
3314 &sensor_dev_attr_pwm3_auto_point1_pwm.dev_attr.attr,
3315 &sensor_dev_attr_pwm3_auto_point2_pwm.dev_attr.attr,
3316 &sensor_dev_attr_pwm3_auto_point3_pwm.dev_attr.attr,
3317 &sensor_dev_attr_pwm3_auto_point4_pwm.dev_attr.attr,
3318 &sensor_dev_attr_pwm3_auto_point5_pwm.dev_attr.attr,
3319 &sensor_dev_attr_pwm3_auto_point6_pwm.dev_attr.attr,
3320 &sensor_dev_attr_pwm3_auto_point7_pwm.dev_attr.attr,
3321 &sensor_dev_attr_pwm3_auto_point8_pwm.dev_attr.attr,
3322 NULL
3323};
3324
3325static umode_t asus_fan_curve_is_visible(struct kobject *kobj,
3326 struct attribute *attr, int idx)
3327{
3328 struct device *dev = kobj_to_dev(kobj);
3329 struct asus_wmi *asus = dev_get_drvdata(dev->parent);
3330
3331 /*
3332 * Check the char instead of casting attr as there are two attr types
3333 * involved here (attr1 and attr2)
3334 */
3335 if (asus->cpu_fan_curve_available && attr->name[3] == '1')
3336 return 0644;
3337
3338 if (asus->gpu_fan_curve_available && attr->name[3] == '2')
3339 return 0644;
3340
3341 if (asus->mid_fan_curve_available && attr->name[3] == '3')
3342 return 0644;
3343
3344 return 0;
3345}
3346
3347static const struct attribute_group asus_fan_curve_attr_group = {
3348 .is_visible = asus_fan_curve_is_visible,
3349 .attrs = asus_fan_curve_attr,
3350};
3351__ATTRIBUTE_GROUPS(asus_fan_curve_attr);
3352
3353/*
3354 * Must be initialised after throttle_thermal_policy_check_present() as
3355 * we check the status of throttle_thermal_policy_available during init.
3356 */
3357static int asus_wmi_custom_fan_curve_init(struct asus_wmi *asus)
3358{
3359 struct device *dev = &asus->platform_device->dev;
3360 struct device *hwmon;
3361 int err;
3362
3363 err = fan_curve_check_present(asus, &asus->cpu_fan_curve_available,
3364 ASUS_WMI_DEVID_CPU_FAN_CURVE);
3365 if (err)
3366 return err;
3367
3368 err = fan_curve_check_present(asus, &asus->gpu_fan_curve_available,
3369 ASUS_WMI_DEVID_GPU_FAN_CURVE);
3370 if (err)
3371 return err;
3372
3373 err = fan_curve_check_present(asus, &asus->mid_fan_curve_available,
3374 ASUS_WMI_DEVID_MID_FAN_CURVE);
3375 if (err)
3376 return err;
3377
3378 if (!asus->cpu_fan_curve_available
3379 && !asus->gpu_fan_curve_available
3380 && !asus->mid_fan_curve_available)
3381 return 0;
3382
3383 hwmon = devm_hwmon_device_register_with_groups(
3384 dev, "asus_custom_fan_curve", asus, asus_fan_curve_attr_groups);
3385
3386 if (IS_ERR(hwmon)) {
3387 dev_err(dev,
3388 "Could not register asus_custom_fan_curve device\n");
3389 return PTR_ERR(hwmon);
3390 }
3391
3392 return 0;
3393}
3394
3395/* Throttle thermal policy ****************************************************/
3396
3397static int throttle_thermal_policy_check_present(struct asus_wmi *asus)
3398{
3399 u32 result;
3400 int err;
3401
3402 asus->throttle_thermal_policy_available = false;
3403
3404 err = asus_wmi_get_devstate(asus,
3405 ASUS_WMI_DEVID_THROTTLE_THERMAL_POLICY,
3406 &result);
3407 if (err) {
3408 if (err == -ENODEV)
3409 return 0;
3410 return err;
3411 }
3412
3413 if (result & ASUS_WMI_DSTS_PRESENCE_BIT)
3414 asus->throttle_thermal_policy_available = true;
3415
3416 return 0;
3417}
3418
3419static int throttle_thermal_policy_write(struct asus_wmi *asus)
3420{
3421 int err;
3422 u8 value;
3423 u32 retval;
3424
3425 value = asus->throttle_thermal_policy_mode;
3426
3427 err = asus_wmi_set_devstate(ASUS_WMI_DEVID_THROTTLE_THERMAL_POLICY,
3428 value, &retval);
3429
3430 sysfs_notify(&asus->platform_device->dev.kobj, NULL,
3431 "throttle_thermal_policy");
3432
3433 if (err) {
3434 pr_warn("Failed to set throttle thermal policy: %d\n", err);
3435 return err;
3436 }
3437
3438 if (retval != 1) {
3439 pr_warn("Failed to set throttle thermal policy (retval): 0x%x\n",
3440 retval);
3441 return -EIO;
3442 }
3443
3444 /* Must set to disabled if mode is toggled */
3445 if (asus->cpu_fan_curve_available)
3446 asus->custom_fan_curves[FAN_CURVE_DEV_CPU].enabled = false;
3447 if (asus->gpu_fan_curve_available)
3448 asus->custom_fan_curves[FAN_CURVE_DEV_GPU].enabled = false;
3449 if (asus->mid_fan_curve_available)
3450 asus->custom_fan_curves[FAN_CURVE_DEV_MID].enabled = false;
3451
3452 return 0;
3453}
3454
3455static int throttle_thermal_policy_set_default(struct asus_wmi *asus)
3456{
3457 if (!asus->throttle_thermal_policy_available)
3458 return 0;
3459
3460 asus->throttle_thermal_policy_mode = ASUS_THROTTLE_THERMAL_POLICY_DEFAULT;
3461 return throttle_thermal_policy_write(asus);
3462}
3463
3464static int throttle_thermal_policy_switch_next(struct asus_wmi *asus)
3465{
3466 u8 new_mode = asus->throttle_thermal_policy_mode + 1;
3467 int err;
3468
3469 if (new_mode > ASUS_THROTTLE_THERMAL_POLICY_SILENT)
3470 new_mode = ASUS_THROTTLE_THERMAL_POLICY_DEFAULT;
3471
3472 asus->throttle_thermal_policy_mode = new_mode;
3473 err = throttle_thermal_policy_write(asus);
3474 if (err)
3475 return err;
3476
3477 /*
3478 * Ensure that platform_profile updates userspace with the change to ensure
3479 * that platform_profile and throttle_thermal_policy_mode are in sync.
3480 */
3481 platform_profile_notify();
3482
3483 return 0;
3484}
3485
3486static ssize_t throttle_thermal_policy_show(struct device *dev,
3487 struct device_attribute *attr, char *buf)
3488{
3489 struct asus_wmi *asus = dev_get_drvdata(dev);
3490 u8 mode = asus->throttle_thermal_policy_mode;
3491
3492 return sysfs_emit(buf, "%d\n", mode);
3493}
3494
3495static ssize_t throttle_thermal_policy_store(struct device *dev,
3496 struct device_attribute *attr,
3497 const char *buf, size_t count)
3498{
3499 struct asus_wmi *asus = dev_get_drvdata(dev);
3500 u8 new_mode;
3501 int result;
3502 int err;
3503
3504 result = kstrtou8(buf, 10, &new_mode);
3505 if (result < 0)
3506 return result;
3507
3508 if (new_mode > ASUS_THROTTLE_THERMAL_POLICY_SILENT)
3509 return -EINVAL;
3510
3511 asus->throttle_thermal_policy_mode = new_mode;
3512 err = throttle_thermal_policy_write(asus);
3513 if (err)
3514 return err;
3515
3516 /*
3517 * Ensure that platform_profile updates userspace with the change to ensure
3518 * that platform_profile and throttle_thermal_policy_mode are in sync.
3519 */
3520 platform_profile_notify();
3521
3522 return count;
3523}
3524
3525// Throttle thermal policy: 0 - default, 1 - overboost, 2 - silent
3526static DEVICE_ATTR_RW(throttle_thermal_policy);
3527
3528/* Platform profile ***********************************************************/
3529static int asus_wmi_platform_profile_get(struct platform_profile_handler *pprof,
3530 enum platform_profile_option *profile)
3531{
3532 struct asus_wmi *asus;
3533 int tp;
3534
3535 asus = container_of(pprof, struct asus_wmi, platform_profile_handler);
3536
3537 tp = asus->throttle_thermal_policy_mode;
3538
3539 switch (tp) {
3540 case ASUS_THROTTLE_THERMAL_POLICY_DEFAULT:
3541 *profile = PLATFORM_PROFILE_BALANCED;
3542 break;
3543 case ASUS_THROTTLE_THERMAL_POLICY_OVERBOOST:
3544 *profile = PLATFORM_PROFILE_PERFORMANCE;
3545 break;
3546 case ASUS_THROTTLE_THERMAL_POLICY_SILENT:
3547 *profile = PLATFORM_PROFILE_QUIET;
3548 break;
3549 default:
3550 return -EINVAL;
3551 }
3552
3553 return 0;
3554}
3555
3556static int asus_wmi_platform_profile_set(struct platform_profile_handler *pprof,
3557 enum platform_profile_option profile)
3558{
3559 struct asus_wmi *asus;
3560 int tp;
3561
3562 asus = container_of(pprof, struct asus_wmi, platform_profile_handler);
3563
3564 switch (profile) {
3565 case PLATFORM_PROFILE_PERFORMANCE:
3566 tp = ASUS_THROTTLE_THERMAL_POLICY_OVERBOOST;
3567 break;
3568 case PLATFORM_PROFILE_BALANCED:
3569 tp = ASUS_THROTTLE_THERMAL_POLICY_DEFAULT;
3570 break;
3571 case PLATFORM_PROFILE_QUIET:
3572 tp = ASUS_THROTTLE_THERMAL_POLICY_SILENT;
3573 break;
3574 default:
3575 return -EOPNOTSUPP;
3576 }
3577
3578 asus->throttle_thermal_policy_mode = tp;
3579 return throttle_thermal_policy_write(asus);
3580}
3581
3582static int platform_profile_setup(struct asus_wmi *asus)
3583{
3584 struct device *dev = &asus->platform_device->dev;
3585 int err;
3586
3587 /*
3588 * Not an error if a component platform_profile relies on is unavailable
3589 * so early return, skipping the setup of platform_profile.
3590 */
3591 if (!asus->throttle_thermal_policy_available)
3592 return 0;
3593
3594 dev_info(dev, "Using throttle_thermal_policy for platform_profile support\n");
3595
3596 asus->platform_profile_handler.profile_get = asus_wmi_platform_profile_get;
3597 asus->platform_profile_handler.profile_set = asus_wmi_platform_profile_set;
3598
3599 set_bit(PLATFORM_PROFILE_QUIET, asus->platform_profile_handler.choices);
3600 set_bit(PLATFORM_PROFILE_BALANCED,
3601 asus->platform_profile_handler.choices);
3602 set_bit(PLATFORM_PROFILE_PERFORMANCE,
3603 asus->platform_profile_handler.choices);
3604
3605 err = platform_profile_register(&asus->platform_profile_handler);
3606 if (err)
3607 return err;
3608
3609 asus->platform_profile_support = true;
3610 return 0;
3611}
3612
3613/* Backlight ******************************************************************/
3614
3615static int read_backlight_power(struct asus_wmi *asus)
3616{
3617 int ret;
3618
3619 if (asus->driver->quirks->store_backlight_power)
3620 ret = !asus->driver->panel_power;
3621 else
3622 ret = asus_wmi_get_devstate_simple(asus,
3623 ASUS_WMI_DEVID_BACKLIGHT);
3624
3625 if (ret < 0)
3626 return ret;
3627
3628 return ret ? FB_BLANK_UNBLANK : FB_BLANK_POWERDOWN;
3629}
3630
3631static int read_brightness_max(struct asus_wmi *asus)
3632{
3633 u32 retval;
3634 int err;
3635
3636 err = asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_BRIGHTNESS, &retval);
3637 if (err < 0)
3638 return err;
3639
3640 retval = retval & ASUS_WMI_DSTS_MAX_BRIGTH_MASK;
3641 retval >>= 8;
3642
3643 if (!retval)
3644 return -ENODEV;
3645
3646 return retval;
3647}
3648
3649static int read_brightness(struct backlight_device *bd)
3650{
3651 struct asus_wmi *asus = bl_get_data(bd);
3652 u32 retval;
3653 int err;
3654
3655 err = asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_BRIGHTNESS, &retval);
3656 if (err < 0)
3657 return err;
3658
3659 return retval & ASUS_WMI_DSTS_BRIGHTNESS_MASK;
3660}
3661
3662static u32 get_scalar_command(struct backlight_device *bd)
3663{
3664 struct asus_wmi *asus = bl_get_data(bd);
3665 u32 ctrl_param = 0;
3666
3667 if ((asus->driver->brightness < bd->props.brightness) ||
3668 bd->props.brightness == bd->props.max_brightness)
3669 ctrl_param = 0x00008001;
3670 else if ((asus->driver->brightness > bd->props.brightness) ||
3671 bd->props.brightness == 0)
3672 ctrl_param = 0x00008000;
3673
3674 asus->driver->brightness = bd->props.brightness;
3675
3676 return ctrl_param;
3677}
3678
3679static int update_bl_status(struct backlight_device *bd)
3680{
3681 struct asus_wmi *asus = bl_get_data(bd);
3682 u32 ctrl_param;
3683 int power, err = 0;
3684
3685 power = read_backlight_power(asus);
3686 if (power != -ENODEV && bd->props.power != power) {
3687 ctrl_param = !!(bd->props.power == FB_BLANK_UNBLANK);
3688 err = asus_wmi_set_devstate(ASUS_WMI_DEVID_BACKLIGHT,
3689 ctrl_param, NULL);
3690 if (asus->driver->quirks->store_backlight_power)
3691 asus->driver->panel_power = bd->props.power;
3692
3693 /* When using scalar brightness, updating the brightness
3694 * will mess with the backlight power */
3695 if (asus->driver->quirks->scalar_panel_brightness)
3696 return err;
3697 }
3698
3699 if (asus->driver->quirks->scalar_panel_brightness)
3700 ctrl_param = get_scalar_command(bd);
3701 else
3702 ctrl_param = bd->props.brightness;
3703
3704 err = asus_wmi_set_devstate(ASUS_WMI_DEVID_BRIGHTNESS,
3705 ctrl_param, NULL);
3706
3707 return err;
3708}
3709
3710static const struct backlight_ops asus_wmi_bl_ops = {
3711 .get_brightness = read_brightness,
3712 .update_status = update_bl_status,
3713};
3714
3715static int asus_wmi_backlight_notify(struct asus_wmi *asus, int code)
3716{
3717 struct backlight_device *bd = asus->backlight_device;
3718 int old = bd->props.brightness;
3719 int new = old;
3720
3721 if (code >= NOTIFY_BRNUP_MIN && code <= NOTIFY_BRNUP_MAX)
3722 new = code - NOTIFY_BRNUP_MIN + 1;
3723 else if (code >= NOTIFY_BRNDOWN_MIN && code <= NOTIFY_BRNDOWN_MAX)
3724 new = code - NOTIFY_BRNDOWN_MIN;
3725
3726 bd->props.brightness = new;
3727 backlight_update_status(bd);
3728 backlight_force_update(bd, BACKLIGHT_UPDATE_HOTKEY);
3729
3730 return old;
3731}
3732
3733static int asus_wmi_backlight_init(struct asus_wmi *asus)
3734{
3735 struct backlight_device *bd;
3736 struct backlight_properties props;
3737 int max;
3738 int power;
3739
3740 max = read_brightness_max(asus);
3741 if (max < 0)
3742 return max;
3743
3744 power = read_backlight_power(asus);
3745 if (power == -ENODEV)
3746 power = FB_BLANK_UNBLANK;
3747 else if (power < 0)
3748 return power;
3749
3750 memset(&props, 0, sizeof(struct backlight_properties));
3751 props.type = BACKLIGHT_PLATFORM;
3752 props.max_brightness = max;
3753 bd = backlight_device_register(asus->driver->name,
3754 &asus->platform_device->dev, asus,
3755 &asus_wmi_bl_ops, &props);
3756 if (IS_ERR(bd)) {
3757 pr_err("Could not register backlight device\n");
3758 return PTR_ERR(bd);
3759 }
3760
3761 asus->backlight_device = bd;
3762
3763 if (asus->driver->quirks->store_backlight_power)
3764 asus->driver->panel_power = power;
3765
3766 bd->props.brightness = read_brightness(bd);
3767 bd->props.power = power;
3768 backlight_update_status(bd);
3769
3770 asus->driver->brightness = bd->props.brightness;
3771
3772 return 0;
3773}
3774
3775static void asus_wmi_backlight_exit(struct asus_wmi *asus)
3776{
3777 backlight_device_unregister(asus->backlight_device);
3778
3779 asus->backlight_device = NULL;
3780}
3781
3782static int is_display_toggle(int code)
3783{
3784 /* display toggle keys */
3785 if ((code >= 0x61 && code <= 0x67) ||
3786 (code >= 0x8c && code <= 0x93) ||
3787 (code >= 0xa0 && code <= 0xa7) ||
3788 (code >= 0xd0 && code <= 0xd5))
3789 return 1;
3790
3791 return 0;
3792}
3793
3794/* Screenpad backlight *******************************************************/
3795
3796static int read_screenpad_backlight_power(struct asus_wmi *asus)
3797{
3798 int ret;
3799
3800 ret = asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_SCREENPAD_POWER);
3801 if (ret < 0)
3802 return ret;
3803 /* 1 == powered */
3804 return ret ? FB_BLANK_UNBLANK : FB_BLANK_POWERDOWN;
3805}
3806
3807static int read_screenpad_brightness(struct backlight_device *bd)
3808{
3809 struct asus_wmi *asus = bl_get_data(bd);
3810 u32 retval;
3811 int err;
3812
3813 err = read_screenpad_backlight_power(asus);
3814 if (err < 0)
3815 return err;
3816 /* The device brightness can only be read if powered, so return stored */
3817 if (err == FB_BLANK_POWERDOWN)
3818 return asus->driver->screenpad_brightness - ASUS_SCREENPAD_BRIGHT_MIN;
3819
3820 err = asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_SCREENPAD_LIGHT, &retval);
3821 if (err < 0)
3822 return err;
3823
3824 return (retval & ASUS_WMI_DSTS_BRIGHTNESS_MASK) - ASUS_SCREENPAD_BRIGHT_MIN;
3825}
3826
3827static int update_screenpad_bl_status(struct backlight_device *bd)
3828{
3829 struct asus_wmi *asus = bl_get_data(bd);
3830 int power, err = 0;
3831 u32 ctrl_param;
3832
3833 power = read_screenpad_backlight_power(asus);
3834 if (power < 0)
3835 return power;
3836
3837 if (bd->props.power != power) {
3838 if (power != FB_BLANK_UNBLANK) {
3839 /* Only brightness > 0 can power it back on */
3840 ctrl_param = asus->driver->screenpad_brightness - ASUS_SCREENPAD_BRIGHT_MIN;
3841 err = asus_wmi_set_devstate(ASUS_WMI_DEVID_SCREENPAD_LIGHT,
3842 ctrl_param, NULL);
3843 } else {
3844 err = asus_wmi_set_devstate(ASUS_WMI_DEVID_SCREENPAD_POWER, 0, NULL);
3845 }
3846 } else if (power == FB_BLANK_UNBLANK) {
3847 /* Only set brightness if powered on or we get invalid/unsync state */
3848 ctrl_param = bd->props.brightness + ASUS_SCREENPAD_BRIGHT_MIN;
3849 err = asus_wmi_set_devstate(ASUS_WMI_DEVID_SCREENPAD_LIGHT, ctrl_param, NULL);
3850 }
3851
3852 /* Ensure brightness is stored to turn back on with */
3853 if (err == 0)
3854 asus->driver->screenpad_brightness = bd->props.brightness + ASUS_SCREENPAD_BRIGHT_MIN;
3855
3856 return err;
3857}
3858
3859static const struct backlight_ops asus_screenpad_bl_ops = {
3860 .get_brightness = read_screenpad_brightness,
3861 .update_status = update_screenpad_bl_status,
3862 .options = BL_CORE_SUSPENDRESUME,
3863};
3864
3865static int asus_screenpad_init(struct asus_wmi *asus)
3866{
3867 struct backlight_device *bd;
3868 struct backlight_properties props;
3869 int err, power;
3870 int brightness = 0;
3871
3872 power = read_screenpad_backlight_power(asus);
3873 if (power < 0)
3874 return power;
3875
3876 if (power != FB_BLANK_POWERDOWN) {
3877 err = asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_SCREENPAD_LIGHT, &brightness);
3878 if (err < 0)
3879 return err;
3880 }
3881 /* default to an acceptable min brightness on boot if too low */
3882 if (brightness < ASUS_SCREENPAD_BRIGHT_MIN)
3883 brightness = ASUS_SCREENPAD_BRIGHT_DEFAULT;
3884
3885 memset(&props, 0, sizeof(struct backlight_properties));
3886 props.type = BACKLIGHT_RAW; /* ensure this bd is last to be picked */
3887 props.max_brightness = ASUS_SCREENPAD_BRIGHT_MAX - ASUS_SCREENPAD_BRIGHT_MIN;
3888 bd = backlight_device_register("asus_screenpad",
3889 &asus->platform_device->dev, asus,
3890 &asus_screenpad_bl_ops, &props);
3891 if (IS_ERR(bd)) {
3892 pr_err("Could not register backlight device\n");
3893 return PTR_ERR(bd);
3894 }
3895
3896 asus->screenpad_backlight_device = bd;
3897 asus->driver->screenpad_brightness = brightness;
3898 bd->props.brightness = brightness - ASUS_SCREENPAD_BRIGHT_MIN;
3899 bd->props.power = power;
3900 backlight_update_status(bd);
3901
3902 return 0;
3903}
3904
3905static void asus_screenpad_exit(struct asus_wmi *asus)
3906{
3907 backlight_device_unregister(asus->screenpad_backlight_device);
3908
3909 asus->screenpad_backlight_device = NULL;
3910}
3911
3912/* Fn-lock ********************************************************************/
3913
3914static bool asus_wmi_has_fnlock_key(struct asus_wmi *asus)
3915{
3916 u32 result;
3917
3918 asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_FNLOCK, &result);
3919
3920 return (result & ASUS_WMI_DSTS_PRESENCE_BIT) &&
3921 !(result & ASUS_WMI_FNLOCK_BIOS_DISABLED);
3922}
3923
3924static void asus_wmi_fnlock_update(struct asus_wmi *asus)
3925{
3926 int mode = asus->fnlock_locked;
3927
3928 asus_wmi_set_devstate(ASUS_WMI_DEVID_FNLOCK, mode, NULL);
3929}
3930
3931/* WMI events *****************************************************************/
3932
3933static int asus_wmi_get_event_code(u32 value)
3934{
3935 struct acpi_buffer response = { ACPI_ALLOCATE_BUFFER, NULL };
3936 union acpi_object *obj;
3937 acpi_status status;
3938 int code;
3939
3940 status = wmi_get_event_data(value, &response);
3941 if (ACPI_FAILURE(status)) {
3942 pr_warn("Failed to get WMI notify code: %s\n",
3943 acpi_format_exception(status));
3944 return -EIO;
3945 }
3946
3947 obj = (union acpi_object *)response.pointer;
3948
3949 if (obj && obj->type == ACPI_TYPE_INTEGER)
3950 code = (int)(obj->integer.value & WMI_EVENT_MASK);
3951 else
3952 code = -EIO;
3953
3954 kfree(obj);
3955 return code;
3956}
3957
3958static void asus_wmi_handle_event_code(int code, struct asus_wmi *asus)
3959{
3960 unsigned int key_value = 1;
3961 bool autorelease = 1;
3962
3963 if (asus->driver->key_filter) {
3964 asus->driver->key_filter(asus->driver, &code, &key_value,
3965 &autorelease);
3966 if (code == ASUS_WMI_KEY_IGNORE)
3967 return;
3968 }
3969
3970 if (acpi_video_get_backlight_type() == acpi_backlight_vendor &&
3971 code >= NOTIFY_BRNUP_MIN && code <= NOTIFY_BRNDOWN_MAX) {
3972 asus_wmi_backlight_notify(asus, code);
3973 return;
3974 }
3975
3976 if (code == NOTIFY_KBD_BRTUP) {
3977 kbd_led_set_by_kbd(asus, asus->kbd_led_wk + 1);
3978 return;
3979 }
3980 if (code == NOTIFY_KBD_BRTDWN) {
3981 kbd_led_set_by_kbd(asus, asus->kbd_led_wk - 1);
3982 return;
3983 }
3984 if (code == NOTIFY_KBD_BRTTOGGLE) {
3985 if (asus->kbd_led_wk == asus->kbd_led.max_brightness)
3986 kbd_led_set_by_kbd(asus, 0);
3987 else
3988 kbd_led_set_by_kbd(asus, asus->kbd_led_wk + 1);
3989 return;
3990 }
3991
3992 if (code == NOTIFY_FNLOCK_TOGGLE) {
3993 asus->fnlock_locked = !asus->fnlock_locked;
3994 asus_wmi_fnlock_update(asus);
3995 return;
3996 }
3997
3998 if (code == asus->tablet_switch_event_code) {
3999 asus_wmi_tablet_mode_get_state(asus);
4000 return;
4001 }
4002
4003 if (code == NOTIFY_KBD_FBM || code == NOTIFY_KBD_TTP) {
4004 if (asus->fan_boost_mode_available)
4005 fan_boost_mode_switch_next(asus);
4006 if (asus->throttle_thermal_policy_available)
4007 throttle_thermal_policy_switch_next(asus);
4008 return;
4009
4010 }
4011
4012 if (is_display_toggle(code) && asus->driver->quirks->no_display_toggle)
4013 return;
4014
4015 if (!sparse_keymap_report_event(asus->inputdev, code,
4016 key_value, autorelease))
4017 pr_info("Unknown key code 0x%x\n", code);
4018}
4019
4020static void asus_wmi_notify(u32 value, void *context)
4021{
4022 struct asus_wmi *asus = context;
4023 int code;
4024 int i;
4025
4026 for (i = 0; i < WMI_EVENT_QUEUE_SIZE + 1; i++) {
4027 code = asus_wmi_get_event_code(value);
4028 if (code < 0) {
4029 pr_warn("Failed to get notify code: %d\n", code);
4030 return;
4031 }
4032
4033 if (code == WMI_EVENT_QUEUE_END || code == WMI_EVENT_MASK)
4034 return;
4035
4036 asus_wmi_handle_event_code(code, asus);
4037
4038 /*
4039 * Double check that queue is present:
4040 * ATK (with queue) uses 0xff, ASUSWMI (without) 0xd2.
4041 */
4042 if (!asus->wmi_event_queue || value != WMI_EVENT_VALUE_ATK)
4043 return;
4044 }
4045
4046 pr_warn("Failed to process event queue, last code: 0x%x\n", code);
4047}
4048
4049static int asus_wmi_notify_queue_flush(struct asus_wmi *asus)
4050{
4051 int code;
4052 int i;
4053
4054 for (i = 0; i < WMI_EVENT_QUEUE_SIZE + 1; i++) {
4055 code = asus_wmi_get_event_code(WMI_EVENT_VALUE_ATK);
4056 if (code < 0) {
4057 pr_warn("Failed to get event during flush: %d\n", code);
4058 return code;
4059 }
4060
4061 if (code == WMI_EVENT_QUEUE_END || code == WMI_EVENT_MASK)
4062 return 0;
4063 }
4064
4065 pr_warn("Failed to flush event queue\n");
4066 return -EIO;
4067}
4068
4069/* Sysfs **********************************************************************/
4070
4071static ssize_t store_sys_wmi(struct asus_wmi *asus, int devid,
4072 const char *buf, size_t count)
4073{
4074 u32 retval;
4075 int err, value;
4076
4077 value = asus_wmi_get_devstate_simple(asus, devid);
4078 if (value < 0)
4079 return value;
4080
4081 err = kstrtoint(buf, 0, &value);
4082 if (err)
4083 return err;
4084
4085 err = asus_wmi_set_devstate(devid, value, &retval);
4086 if (err < 0)
4087 return err;
4088
4089 return count;
4090}
4091
4092static ssize_t show_sys_wmi(struct asus_wmi *asus, int devid, char *buf)
4093{
4094 int value = asus_wmi_get_devstate_simple(asus, devid);
4095
4096 if (value < 0)
4097 return value;
4098
4099 return sprintf(buf, "%d\n", value);
4100}
4101
4102#define ASUS_WMI_CREATE_DEVICE_ATTR(_name, _mode, _cm) \
4103 static ssize_t show_##_name(struct device *dev, \
4104 struct device_attribute *attr, \
4105 char *buf) \
4106 { \
4107 struct asus_wmi *asus = dev_get_drvdata(dev); \
4108 \
4109 return show_sys_wmi(asus, _cm, buf); \
4110 } \
4111 static ssize_t store_##_name(struct device *dev, \
4112 struct device_attribute *attr, \
4113 const char *buf, size_t count) \
4114 { \
4115 struct asus_wmi *asus = dev_get_drvdata(dev); \
4116 \
4117 return store_sys_wmi(asus, _cm, buf, count); \
4118 } \
4119 static struct device_attribute dev_attr_##_name = { \
4120 .attr = { \
4121 .name = __stringify(_name), \
4122 .mode = _mode }, \
4123 .show = show_##_name, \
4124 .store = store_##_name, \
4125 }
4126
4127ASUS_WMI_CREATE_DEVICE_ATTR(touchpad, 0644, ASUS_WMI_DEVID_TOUCHPAD);
4128ASUS_WMI_CREATE_DEVICE_ATTR(camera, 0644, ASUS_WMI_DEVID_CAMERA);
4129ASUS_WMI_CREATE_DEVICE_ATTR(cardr, 0644, ASUS_WMI_DEVID_CARDREADER);
4130ASUS_WMI_CREATE_DEVICE_ATTR(lid_resume, 0644, ASUS_WMI_DEVID_LID_RESUME);
4131ASUS_WMI_CREATE_DEVICE_ATTR(als_enable, 0644, ASUS_WMI_DEVID_ALS_ENABLE);
4132
4133static ssize_t cpufv_store(struct device *dev, struct device_attribute *attr,
4134 const char *buf, size_t count)
4135{
4136 int value, rv;
4137
4138 rv = kstrtoint(buf, 0, &value);
4139 if (rv)
4140 return rv;
4141
4142 if (value < 0 || value > 2)
4143 return -EINVAL;
4144
4145 rv = asus_wmi_evaluate_method(ASUS_WMI_METHODID_CFVS, value, 0, NULL);
4146 if (rv < 0)
4147 return rv;
4148
4149 return count;
4150}
4151
4152static DEVICE_ATTR_WO(cpufv);
4153
4154static struct attribute *platform_attributes[] = {
4155 &dev_attr_cpufv.attr,
4156 &dev_attr_camera.attr,
4157 &dev_attr_cardr.attr,
4158 &dev_attr_touchpad.attr,
4159 &dev_attr_charge_mode.attr,
4160 &dev_attr_egpu_enable.attr,
4161 &dev_attr_egpu_connected.attr,
4162 &dev_attr_dgpu_disable.attr,
4163 &dev_attr_gpu_mux_mode.attr,
4164 &dev_attr_lid_resume.attr,
4165 &dev_attr_als_enable.attr,
4166 &dev_attr_fan_boost_mode.attr,
4167 &dev_attr_throttle_thermal_policy.attr,
4168 &dev_attr_ppt_pl2_sppt.attr,
4169 &dev_attr_ppt_pl1_spl.attr,
4170 &dev_attr_ppt_fppt.attr,
4171 &dev_attr_ppt_apu_sppt.attr,
4172 &dev_attr_ppt_platform_sppt.attr,
4173 &dev_attr_nv_dynamic_boost.attr,
4174 &dev_attr_nv_temp_target.attr,
4175 &dev_attr_panel_od.attr,
4176 &dev_attr_mini_led_mode.attr,
4177 NULL
4178};
4179
4180static umode_t asus_sysfs_is_visible(struct kobject *kobj,
4181 struct attribute *attr, int idx)
4182{
4183 struct device *dev = kobj_to_dev(kobj);
4184 struct asus_wmi *asus = dev_get_drvdata(dev);
4185 bool ok = true;
4186 int devid = -1;
4187
4188 if (attr == &dev_attr_camera.attr)
4189 devid = ASUS_WMI_DEVID_CAMERA;
4190 else if (attr == &dev_attr_cardr.attr)
4191 devid = ASUS_WMI_DEVID_CARDREADER;
4192 else if (attr == &dev_attr_touchpad.attr)
4193 devid = ASUS_WMI_DEVID_TOUCHPAD;
4194 else if (attr == &dev_attr_lid_resume.attr)
4195 devid = ASUS_WMI_DEVID_LID_RESUME;
4196 else if (attr == &dev_attr_als_enable.attr)
4197 devid = ASUS_WMI_DEVID_ALS_ENABLE;
4198 else if (attr == &dev_attr_charge_mode.attr)
4199 ok = asus->charge_mode_available;
4200 else if (attr == &dev_attr_egpu_enable.attr)
4201 ok = asus->egpu_enable_available;
4202 else if (attr == &dev_attr_egpu_connected.attr)
4203 ok = asus->egpu_connect_available;
4204 else if (attr == &dev_attr_dgpu_disable.attr)
4205 ok = asus->dgpu_disable_available;
4206 else if (attr == &dev_attr_gpu_mux_mode.attr)
4207 ok = asus->gpu_mux_mode_available;
4208 else if (attr == &dev_attr_fan_boost_mode.attr)
4209 ok = asus->fan_boost_mode_available;
4210 else if (attr == &dev_attr_throttle_thermal_policy.attr)
4211 ok = asus->throttle_thermal_policy_available;
4212 else if (attr == &dev_attr_ppt_pl2_sppt.attr)
4213 ok = asus->ppt_pl2_sppt_available;
4214 else if (attr == &dev_attr_ppt_pl1_spl.attr)
4215 ok = asus->ppt_pl1_spl_available;
4216 else if (attr == &dev_attr_ppt_fppt.attr)
4217 ok = asus->ppt_fppt_available;
4218 else if (attr == &dev_attr_ppt_apu_sppt.attr)
4219 ok = asus->ppt_apu_sppt_available;
4220 else if (attr == &dev_attr_ppt_platform_sppt.attr)
4221 ok = asus->ppt_plat_sppt_available;
4222 else if (attr == &dev_attr_nv_dynamic_boost.attr)
4223 ok = asus->nv_dyn_boost_available;
4224 else if (attr == &dev_attr_nv_temp_target.attr)
4225 ok = asus->nv_temp_tgt_available;
4226 else if (attr == &dev_attr_panel_od.attr)
4227 ok = asus->panel_overdrive_available;
4228 else if (attr == &dev_attr_mini_led_mode.attr)
4229 ok = asus->mini_led_mode_available;
4230
4231 if (devid != -1)
4232 ok = !(asus_wmi_get_devstate_simple(asus, devid) < 0);
4233
4234 return ok ? attr->mode : 0;
4235}
4236
4237static const struct attribute_group platform_attribute_group = {
4238 .is_visible = asus_sysfs_is_visible,
4239 .attrs = platform_attributes
4240};
4241
4242static void asus_wmi_sysfs_exit(struct platform_device *device)
4243{
4244 sysfs_remove_group(&device->dev.kobj, &platform_attribute_group);
4245}
4246
4247static int asus_wmi_sysfs_init(struct platform_device *device)
4248{
4249 return sysfs_create_group(&device->dev.kobj, &platform_attribute_group);
4250}
4251
4252/* Platform device ************************************************************/
4253
4254static int asus_wmi_platform_init(struct asus_wmi *asus)
4255{
4256 struct device *dev = &asus->platform_device->dev;
4257 char *wmi_uid;
4258 int rv;
4259
4260 /* INIT enable hotkeys on some models */
4261 if (!asus_wmi_evaluate_method(ASUS_WMI_METHODID_INIT, 0, 0, &rv))
4262 pr_info("Initialization: %#x\n", rv);
4263
4264 /* We don't know yet what to do with this version... */
4265 if (!asus_wmi_evaluate_method(ASUS_WMI_METHODID_SPEC, 0, 0x9, &rv)) {
4266 pr_info("BIOS WMI version: %d.%d\n", rv >> 16, rv & 0xFF);
4267 asus->spec = rv;
4268 }
4269
4270 /*
4271 * The SFUN method probably allows the original driver to get the list
4272 * of features supported by a given model. For now, 0x0100 or 0x0800
4273 * bit signifies that the laptop is equipped with a Wi-Fi MiniPCI card.
4274 * The significance of others is yet to be found.
4275 */
4276 if (!asus_wmi_evaluate_method(ASUS_WMI_METHODID_SFUN, 0, 0, &rv)) {
4277 pr_info("SFUN value: %#x\n", rv);
4278 asus->sfun = rv;
4279 }
4280
4281 /*
4282 * Eee PC and Notebooks seems to have different method_id for DSTS,
4283 * but it may also be related to the BIOS's SPEC.
4284 * Note, on most Eeepc, there is no way to check if a method exist
4285 * or note, while on notebooks, they returns 0xFFFFFFFE on failure,
4286 * but once again, SPEC may probably be used for that kind of things.
4287 *
4288 * Additionally at least TUF Gaming series laptops return nothing for
4289 * unknown methods, so the detection in this way is not possible.
4290 *
4291 * There is strong indication that only ACPI WMI devices that have _UID
4292 * equal to "ASUSWMI" use DCTS whereas those with "ATK" use DSTS.
4293 */
4294 wmi_uid = wmi_get_acpi_device_uid(ASUS_WMI_MGMT_GUID);
4295 if (!wmi_uid)
4296 return -ENODEV;
4297
4298 if (!strcmp(wmi_uid, ASUS_ACPI_UID_ASUSWMI)) {
4299 dev_info(dev, "Detected ASUSWMI, use DCTS\n");
4300 asus->dsts_id = ASUS_WMI_METHODID_DCTS;
4301 } else {
4302 dev_info(dev, "Detected %s, not ASUSWMI, use DSTS\n", wmi_uid);
4303 asus->dsts_id = ASUS_WMI_METHODID_DSTS;
4304 }
4305
4306 /*
4307 * Some devices can have multiple event codes stored in a queue before
4308 * the module load if it was unloaded intermittently after calling
4309 * the INIT method (enables event handling). The WMI notify handler is
4310 * expected to retrieve all event codes until a retrieved code equals
4311 * queue end marker (One or Ones). Old codes are flushed from the queue
4312 * upon module load. Not enabling this when it should be has minimal
4313 * visible impact so fall back if anything goes wrong.
4314 */
4315 wmi_uid = wmi_get_acpi_device_uid(asus->driver->event_guid);
4316 if (wmi_uid && !strcmp(wmi_uid, ASUS_ACPI_UID_ATK)) {
4317 dev_info(dev, "Detected ATK, enable event queue\n");
4318
4319 if (!asus_wmi_notify_queue_flush(asus))
4320 asus->wmi_event_queue = true;
4321 }
4322
4323 /* CWAP allow to define the behavior of the Fn+F2 key,
4324 * this method doesn't seems to be present on Eee PCs */
4325 if (asus->driver->quirks->wapf >= 0)
4326 asus_wmi_set_devstate(ASUS_WMI_DEVID_CWAP,
4327 asus->driver->quirks->wapf, NULL);
4328
4329 return 0;
4330}
4331
4332/* debugfs ********************************************************************/
4333
4334struct asus_wmi_debugfs_node {
4335 struct asus_wmi *asus;
4336 char *name;
4337 int (*show) (struct seq_file *m, void *data);
4338};
4339
4340static int show_dsts(struct seq_file *m, void *data)
4341{
4342 struct asus_wmi *asus = m->private;
4343 int err;
4344 u32 retval = -1;
4345
4346 err = asus_wmi_get_devstate(asus, asus->debug.dev_id, &retval);
4347 if (err < 0)
4348 return err;
4349
4350 seq_printf(m, "DSTS(%#x) = %#x\n", asus->debug.dev_id, retval);
4351
4352 return 0;
4353}
4354
4355static int show_devs(struct seq_file *m, void *data)
4356{
4357 struct asus_wmi *asus = m->private;
4358 int err;
4359 u32 retval = -1;
4360
4361 err = asus_wmi_set_devstate(asus->debug.dev_id, asus->debug.ctrl_param,
4362 &retval);
4363 if (err < 0)
4364 return err;
4365
4366 seq_printf(m, "DEVS(%#x, %#x) = %#x\n", asus->debug.dev_id,
4367 asus->debug.ctrl_param, retval);
4368
4369 return 0;
4370}
4371
4372static int show_call(struct seq_file *m, void *data)
4373{
4374 struct asus_wmi *asus = m->private;
4375 struct bios_args args = {
4376 .arg0 = asus->debug.dev_id,
4377 .arg1 = asus->debug.ctrl_param,
4378 };
4379 struct acpi_buffer input = { (acpi_size) sizeof(args), &args };
4380 struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER, NULL };
4381 union acpi_object *obj;
4382 acpi_status status;
4383
4384 status = wmi_evaluate_method(ASUS_WMI_MGMT_GUID,
4385 0, asus->debug.method_id,
4386 &input, &output);
4387
4388 if (ACPI_FAILURE(status))
4389 return -EIO;
4390
4391 obj = (union acpi_object *)output.pointer;
4392 if (obj && obj->type == ACPI_TYPE_INTEGER)
4393 seq_printf(m, "%#x(%#x, %#x) = %#x\n", asus->debug.method_id,
4394 asus->debug.dev_id, asus->debug.ctrl_param,
4395 (u32) obj->integer.value);
4396 else
4397 seq_printf(m, "%#x(%#x, %#x) = t:%d\n", asus->debug.method_id,
4398 asus->debug.dev_id, asus->debug.ctrl_param,
4399 obj ? obj->type : -1);
4400
4401 kfree(obj);
4402
4403 return 0;
4404}
4405
4406static struct asus_wmi_debugfs_node asus_wmi_debug_files[] = {
4407 {NULL, "devs", show_devs},
4408 {NULL, "dsts", show_dsts},
4409 {NULL, "call", show_call},
4410};
4411
4412static int asus_wmi_debugfs_open(struct inode *inode, struct file *file)
4413{
4414 struct asus_wmi_debugfs_node *node = inode->i_private;
4415
4416 return single_open(file, node->show, node->asus);
4417}
4418
4419static const struct file_operations asus_wmi_debugfs_io_ops = {
4420 .owner = THIS_MODULE,
4421 .open = asus_wmi_debugfs_open,
4422 .read = seq_read,
4423 .llseek = seq_lseek,
4424 .release = single_release,
4425};
4426
4427static void asus_wmi_debugfs_exit(struct asus_wmi *asus)
4428{
4429 debugfs_remove_recursive(asus->debug.root);
4430}
4431
4432static void asus_wmi_debugfs_init(struct asus_wmi *asus)
4433{
4434 int i;
4435
4436 asus->debug.root = debugfs_create_dir(asus->driver->name, NULL);
4437
4438 debugfs_create_x32("method_id", S_IRUGO | S_IWUSR, asus->debug.root,
4439 &asus->debug.method_id);
4440
4441 debugfs_create_x32("dev_id", S_IRUGO | S_IWUSR, asus->debug.root,
4442 &asus->debug.dev_id);
4443
4444 debugfs_create_x32("ctrl_param", S_IRUGO | S_IWUSR, asus->debug.root,
4445 &asus->debug.ctrl_param);
4446
4447 for (i = 0; i < ARRAY_SIZE(asus_wmi_debug_files); i++) {
4448 struct asus_wmi_debugfs_node *node = &asus_wmi_debug_files[i];
4449
4450 node->asus = asus;
4451 debugfs_create_file(node->name, S_IFREG | S_IRUGO,
4452 asus->debug.root, node,
4453 &asus_wmi_debugfs_io_ops);
4454 }
4455}
4456
4457/* Init / exit ****************************************************************/
4458
4459static int asus_wmi_add(struct platform_device *pdev)
4460{
4461 struct platform_driver *pdrv = to_platform_driver(pdev->dev.driver);
4462 struct asus_wmi_driver *wdrv = to_asus_wmi_driver(pdrv);
4463 struct asus_wmi *asus;
4464 acpi_status status;
4465 int err;
4466 u32 result;
4467
4468 asus = kzalloc(sizeof(struct asus_wmi), GFP_KERNEL);
4469 if (!asus)
4470 return -ENOMEM;
4471
4472 asus->driver = wdrv;
4473 asus->platform_device = pdev;
4474 wdrv->platform_device = pdev;
4475 platform_set_drvdata(asus->platform_device, asus);
4476
4477 if (wdrv->detect_quirks)
4478 wdrv->detect_quirks(asus->driver);
4479
4480 err = asus_wmi_platform_init(asus);
4481 if (err)
4482 goto fail_platform;
4483
4484 asus->charge_mode_available = asus_wmi_dev_is_present(asus, ASUS_WMI_DEVID_CHARGE_MODE);
4485 asus->egpu_enable_available = asus_wmi_dev_is_present(asus, ASUS_WMI_DEVID_EGPU);
4486 asus->egpu_connect_available = asus_wmi_dev_is_present(asus, ASUS_WMI_DEVID_EGPU_CONNECTED);
4487 asus->dgpu_disable_available = asus_wmi_dev_is_present(asus, ASUS_WMI_DEVID_DGPU);
4488 asus->gpu_mux_mode_available = asus_wmi_dev_is_present(asus, ASUS_WMI_DEVID_GPU_MUX);
4489 asus->kbd_rgb_mode_available = asus_wmi_dev_is_present(asus, ASUS_WMI_DEVID_TUF_RGB_MODE);
4490 asus->kbd_rgb_state_available = asus_wmi_dev_is_present(asus, ASUS_WMI_DEVID_TUF_RGB_STATE);
4491 asus->ppt_pl2_sppt_available = asus_wmi_dev_is_present(asus, ASUS_WMI_DEVID_PPT_PL2_SPPT);
4492 asus->ppt_pl1_spl_available = asus_wmi_dev_is_present(asus, ASUS_WMI_DEVID_PPT_PL1_SPL);
4493 asus->ppt_fppt_available = asus_wmi_dev_is_present(asus, ASUS_WMI_DEVID_PPT_FPPT);
4494 asus->ppt_apu_sppt_available = asus_wmi_dev_is_present(asus, ASUS_WMI_DEVID_PPT_APU_SPPT);
4495 asus->ppt_plat_sppt_available = asus_wmi_dev_is_present(asus, ASUS_WMI_DEVID_PPT_PLAT_SPPT);
4496 asus->nv_dyn_boost_available = asus_wmi_dev_is_present(asus, ASUS_WMI_DEVID_NV_DYN_BOOST);
4497 asus->nv_temp_tgt_available = asus_wmi_dev_is_present(asus, ASUS_WMI_DEVID_NV_THERM_TARGET);
4498 asus->panel_overdrive_available = asus_wmi_dev_is_present(asus, ASUS_WMI_DEVID_PANEL_OD);
4499 asus->mini_led_mode_available = asus_wmi_dev_is_present(asus, ASUS_WMI_DEVID_MINI_LED_MODE);
4500 asus->ally_mcu_usb_switch = acpi_has_method(NULL, ASUS_USB0_PWR_EC0_CSEE)
4501 && dmi_match(DMI_BOARD_NAME, "RC71L");
4502
4503 err = fan_boost_mode_check_present(asus);
4504 if (err)
4505 goto fail_fan_boost_mode;
4506
4507 err = throttle_thermal_policy_check_present(asus);
4508 if (err)
4509 goto fail_throttle_thermal_policy;
4510 else
4511 throttle_thermal_policy_set_default(asus);
4512
4513 err = platform_profile_setup(asus);
4514 if (err)
4515 goto fail_platform_profile_setup;
4516
4517 err = asus_wmi_sysfs_init(asus->platform_device);
4518 if (err)
4519 goto fail_sysfs;
4520
4521 err = asus_wmi_input_init(asus);
4522 if (err)
4523 goto fail_input;
4524
4525 err = asus_wmi_fan_init(asus); /* probably no problems on error */
4526
4527 err = asus_wmi_hwmon_init(asus);
4528 if (err)
4529 goto fail_hwmon;
4530
4531 err = asus_wmi_custom_fan_curve_init(asus);
4532 if (err)
4533 goto fail_custom_fan_curve;
4534
4535 err = asus_wmi_led_init(asus);
4536 if (err)
4537 goto fail_leds;
4538
4539 asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_WLAN, &result);
4540 if (result & (ASUS_WMI_DSTS_PRESENCE_BIT | ASUS_WMI_DSTS_USER_BIT))
4541 asus->driver->wlan_ctrl_by_user = 1;
4542
4543 if (!(asus->driver->wlan_ctrl_by_user && ashs_present())) {
4544 err = asus_wmi_rfkill_init(asus);
4545 if (err)
4546 goto fail_rfkill;
4547 }
4548
4549 if (asus->driver->quirks->wmi_force_als_set)
4550 asus_wmi_set_als();
4551
4552 if (asus->driver->quirks->xusb2pr)
4553 asus_wmi_set_xusb2pr(asus);
4554
4555 if (acpi_video_get_backlight_type() == acpi_backlight_vendor) {
4556 err = asus_wmi_backlight_init(asus);
4557 if (err && err != -ENODEV)
4558 goto fail_backlight;
4559 } else if (asus->driver->quirks->wmi_backlight_set_devstate)
4560 err = asus_wmi_set_devstate(ASUS_WMI_DEVID_BACKLIGHT, 2, NULL);
4561
4562 if (asus_wmi_dev_is_present(asus, ASUS_WMI_DEVID_SCREENPAD_LIGHT)) {
4563 err = asus_screenpad_init(asus);
4564 if (err && err != -ENODEV)
4565 goto fail_screenpad;
4566 }
4567
4568 if (asus_wmi_has_fnlock_key(asus)) {
4569 asus->fnlock_locked = fnlock_default;
4570 asus_wmi_fnlock_update(asus);
4571 }
4572
4573 status = wmi_install_notify_handler(asus->driver->event_guid,
4574 asus_wmi_notify, asus);
4575 if (ACPI_FAILURE(status)) {
4576 pr_err("Unable to register notify handler - %d\n", status);
4577 err = -ENODEV;
4578 goto fail_wmi_handler;
4579 }
4580
4581 if (asus->driver->i8042_filter) {
4582 err = i8042_install_filter(asus->driver->i8042_filter);
4583 if (err)
4584 pr_warn("Unable to install key filter - %d\n", err);
4585 }
4586
4587 asus_wmi_battery_init(asus);
4588
4589 asus_wmi_debugfs_init(asus);
4590
4591 return 0;
4592
4593fail_wmi_handler:
4594 asus_wmi_backlight_exit(asus);
4595fail_backlight:
4596 asus_wmi_rfkill_exit(asus);
4597fail_screenpad:
4598 asus_screenpad_exit(asus);
4599fail_rfkill:
4600 asus_wmi_led_exit(asus);
4601fail_leds:
4602fail_hwmon:
4603 asus_wmi_input_exit(asus);
4604fail_input:
4605 asus_wmi_sysfs_exit(asus->platform_device);
4606fail_sysfs:
4607fail_throttle_thermal_policy:
4608fail_custom_fan_curve:
4609fail_platform_profile_setup:
4610 if (asus->platform_profile_support)
4611 platform_profile_remove();
4612fail_fan_boost_mode:
4613fail_platform:
4614 kfree(asus);
4615 return err;
4616}
4617
4618static void asus_wmi_remove(struct platform_device *device)
4619{
4620 struct asus_wmi *asus;
4621
4622 asus = platform_get_drvdata(device);
4623 if (asus->driver->i8042_filter)
4624 i8042_remove_filter(asus->driver->i8042_filter);
4625 wmi_remove_notify_handler(asus->driver->event_guid);
4626 asus_wmi_backlight_exit(asus);
4627 asus_screenpad_exit(asus);
4628 asus_wmi_input_exit(asus);
4629 asus_wmi_led_exit(asus);
4630 asus_wmi_rfkill_exit(asus);
4631 asus_wmi_debugfs_exit(asus);
4632 asus_wmi_sysfs_exit(asus->platform_device);
4633 asus_fan_set_auto(asus);
4634 throttle_thermal_policy_set_default(asus);
4635 asus_wmi_battery_exit(asus);
4636
4637 if (asus->platform_profile_support)
4638 platform_profile_remove();
4639
4640 kfree(asus);
4641}
4642
4643/* Platform driver - hibernate/resume callbacks *******************************/
4644
4645static int asus_hotk_thaw(struct device *device)
4646{
4647 struct asus_wmi *asus = dev_get_drvdata(device);
4648
4649 if (asus->wlan.rfkill) {
4650 bool wlan;
4651
4652 /*
4653 * Work around bios bug - acpi _PTS turns off the wireless led
4654 * during suspend. Normally it restores it on resume, but
4655 * we should kick it ourselves in case hibernation is aborted.
4656 */
4657 wlan = asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_WLAN);
4658 asus_wmi_set_devstate(ASUS_WMI_DEVID_WLAN, wlan, NULL);
4659 }
4660
4661 return 0;
4662}
4663
4664static int asus_hotk_resume(struct device *device)
4665{
4666 struct asus_wmi *asus = dev_get_drvdata(device);
4667
4668 if (!IS_ERR_OR_NULL(asus->kbd_led.dev))
4669 kbd_led_update(asus);
4670
4671 if (asus_wmi_has_fnlock_key(asus))
4672 asus_wmi_fnlock_update(asus);
4673
4674 asus_wmi_tablet_mode_get_state(asus);
4675
4676 return 0;
4677}
4678
4679static int asus_hotk_resume_early(struct device *device)
4680{
4681 struct asus_wmi *asus = dev_get_drvdata(device);
4682
4683 if (asus->ally_mcu_usb_switch) {
4684 if (ACPI_FAILURE(acpi_execute_simple_method(NULL, ASUS_USB0_PWR_EC0_CSEE, 0xB8)))
4685 dev_err(device, "ROG Ally MCU failed to connect USB dev\n");
4686 else
4687 msleep(ASUS_USB0_PWR_EC0_CSEE_WAIT);
4688 }
4689 return 0;
4690}
4691
4692static int asus_hotk_prepare(struct device *device)
4693{
4694 struct asus_wmi *asus = dev_get_drvdata(device);
4695 int result, err;
4696
4697 if (asus->ally_mcu_usb_switch) {
4698 /* When powersave is enabled it causes many issues with resume of USB hub */
4699 result = asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_MCU_POWERSAVE);
4700 if (result == 1) {
4701 dev_warn(device, "MCU powersave enabled, disabling to prevent resume issues");
4702 err = asus_wmi_set_devstate(ASUS_WMI_DEVID_MCU_POWERSAVE, 0, &result);
4703 if (err || result != 1)
4704 dev_err(device, "Failed to set MCU powersave mode: %d\n", err);
4705 }
4706 /* sleep required to ensure USB0 is disabled before sleep continues */
4707 if (ACPI_FAILURE(acpi_execute_simple_method(NULL, ASUS_USB0_PWR_EC0_CSEE, 0xB7)))
4708 dev_err(device, "ROG Ally MCU failed to disconnect USB dev\n");
4709 else
4710 msleep(ASUS_USB0_PWR_EC0_CSEE_WAIT);
4711 }
4712 return 0;
4713}
4714
4715static int asus_hotk_restore(struct device *device)
4716{
4717 struct asus_wmi *asus = dev_get_drvdata(device);
4718 int bl;
4719
4720 /* Refresh both wlan rfkill state and pci hotplug */
4721 if (asus->wlan.rfkill)
4722 asus_rfkill_hotplug(asus);
4723
4724 if (asus->bluetooth.rfkill) {
4725 bl = !asus_wmi_get_devstate_simple(asus,
4726 ASUS_WMI_DEVID_BLUETOOTH);
4727 rfkill_set_sw_state(asus->bluetooth.rfkill, bl);
4728 }
4729 if (asus->wimax.rfkill) {
4730 bl = !asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_WIMAX);
4731 rfkill_set_sw_state(asus->wimax.rfkill, bl);
4732 }
4733 if (asus->wwan3g.rfkill) {
4734 bl = !asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_WWAN3G);
4735 rfkill_set_sw_state(asus->wwan3g.rfkill, bl);
4736 }
4737 if (asus->gps.rfkill) {
4738 bl = !asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_GPS);
4739 rfkill_set_sw_state(asus->gps.rfkill, bl);
4740 }
4741 if (asus->uwb.rfkill) {
4742 bl = !asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_UWB);
4743 rfkill_set_sw_state(asus->uwb.rfkill, bl);
4744 }
4745 if (!IS_ERR_OR_NULL(asus->kbd_led.dev))
4746 kbd_led_update(asus);
4747
4748 if (asus_wmi_has_fnlock_key(asus))
4749 asus_wmi_fnlock_update(asus);
4750
4751 asus_wmi_tablet_mode_get_state(asus);
4752 return 0;
4753}
4754
4755static const struct dev_pm_ops asus_pm_ops = {
4756 .thaw = asus_hotk_thaw,
4757 .restore = asus_hotk_restore,
4758 .resume = asus_hotk_resume,
4759 .resume_early = asus_hotk_resume_early,
4760 .prepare = asus_hotk_prepare,
4761};
4762
4763/* Registration ***************************************************************/
4764
4765static int asus_wmi_probe(struct platform_device *pdev)
4766{
4767 struct platform_driver *pdrv = to_platform_driver(pdev->dev.driver);
4768 struct asus_wmi_driver *wdrv = to_asus_wmi_driver(pdrv);
4769 int ret;
4770
4771 if (!wmi_has_guid(ASUS_WMI_MGMT_GUID)) {
4772 pr_warn("ASUS Management GUID not found\n");
4773 return -ENODEV;
4774 }
4775
4776 if (wdrv->event_guid && !wmi_has_guid(wdrv->event_guid)) {
4777 pr_warn("ASUS Event GUID not found\n");
4778 return -ENODEV;
4779 }
4780
4781 if (wdrv->probe) {
4782 ret = wdrv->probe(pdev);
4783 if (ret)
4784 return ret;
4785 }
4786
4787 return asus_wmi_add(pdev);
4788}
4789
4790static bool used;
4791
4792int __init_or_module asus_wmi_register_driver(struct asus_wmi_driver *driver)
4793{
4794 struct platform_driver *platform_driver;
4795 struct platform_device *platform_device;
4796
4797 if (used)
4798 return -EBUSY;
4799
4800 platform_driver = &driver->platform_driver;
4801 platform_driver->remove_new = asus_wmi_remove;
4802 platform_driver->driver.owner = driver->owner;
4803 platform_driver->driver.name = driver->name;
4804 platform_driver->driver.pm = &asus_pm_ops;
4805
4806 platform_device = platform_create_bundle(platform_driver,
4807 asus_wmi_probe,
4808 NULL, 0, NULL, 0);
4809 if (IS_ERR(platform_device))
4810 return PTR_ERR(platform_device);
4811
4812 used = true;
4813 return 0;
4814}
4815EXPORT_SYMBOL_GPL(asus_wmi_register_driver);
4816
4817void asus_wmi_unregister_driver(struct asus_wmi_driver *driver)
4818{
4819 platform_device_unregister(driver->platform_device);
4820 platform_driver_unregister(&driver->platform_driver);
4821 used = false;
4822}
4823EXPORT_SYMBOL_GPL(asus_wmi_unregister_driver);
4824
4825static int __init asus_wmi_init(void)
4826{
4827 pr_info("ASUS WMI generic driver loaded\n");
4828 return 0;
4829}
4830
4831static void __exit asus_wmi_exit(void)
4832{
4833 pr_info("ASUS WMI generic driver unloaded\n");
4834}
4835
4836module_init(asus_wmi_init);
4837module_exit(asus_wmi_exit);