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