blob: 22ae350e0a968a3da67a16e47c83270b9b75b197 [file] [log] [blame]
Thomas Gleixner1a59d1b82019-05-27 08:55:05 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Yong Wangee027e42010-03-21 10:26:34 +08002/*
Corentin Charye12e6d92011-02-26 10:20:31 +01003 * Asus PC WMI hotkey driver
Yong Wangee027e42010-03-21 10:26:34 +08004 *
5 * Copyright(C) 2010 Intel Corporation.
Corentin Chary57ab7da2011-02-26 10:20:32 +01006 * Copyright(C) 2010-2011 Corentin Chary <corentin.chary@gmail.com>
Yong Wangee027e42010-03-21 10:26:34 +08007 *
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>
Yong Wangee027e42010-03-21 10:26:34 +080012 */
13
Yong Wang81248882010-04-11 09:26:33 +080014#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
15
Yong Wangee027e42010-03-21 10:26:34 +080016#include <linux/kernel.h>
17#include <linux/module.h>
18#include <linux/init.h>
19#include <linux/types.h>
Tejun Heoa32f3922010-04-05 11:37:59 +090020#include <linux/slab.h>
Yong Wangee027e42010-03-21 10:26:34 +080021#include <linux/input.h>
22#include <linux/input/sparse-keymap.h>
Yong Wang3d7b1652010-04-11 09:27:54 +080023#include <linux/fb.h>
24#include <linux/backlight.h>
Corentin Chary084fca62010-11-29 08:14:06 +010025#include <linux/leds.h>
Corentin Charyba48fdb2010-11-29 08:14:07 +010026#include <linux/rfkill.h>
Corentin Charyafa7c882011-02-06 13:28:28 +010027#include <linux/pci.h>
28#include <linux/pci_hotplug.h>
Corentin Charye07babd2011-02-26 10:20:42 +010029#include <linux/hwmon.h>
30#include <linux/hwmon-sysfs.h>
Corentin Chary8c1b2d82010-11-29 08:14:09 +010031#include <linux/debugfs.h>
32#include <linux/seq_file.h>
Daniel Drakeffb6ce72018-10-09 14:40:55 +080033#include <linux/platform_data/x86/asus-wmi.h>
Yong Wang45f2c692010-04-11 09:27:19 +080034#include <linux/platform_device.h>
Corentin Chary6118b8a2011-07-01 11:34:36 +020035#include <linux/thermal.h>
Lv Zheng8b484632013-12-03 08:49:16 +080036#include <linux/acpi.h>
Hans de Goeded8c66f62014-07-08 10:47:21 +020037#include <linux/dmi.h>
AceLan Kao272c77d2012-06-13 09:32:06 +020038#include <acpi/video.h>
Yong Wangee027e42010-03-21 10:26:34 +080039
Corentin Charye12e6d92011-02-26 10:20:31 +010040#include "asus-wmi.h"
Yong Wang45f2c692010-04-11 09:27:19 +080041
Corentin Chary5909c652012-12-17 16:00:05 -080042MODULE_AUTHOR("Corentin Chary <corentin.chary@gmail.com>, "
Corentin Charye12e6d92011-02-26 10:20:31 +010043 "Yong Wang <yong.y.wang@intel.com>");
44MODULE_DESCRIPTION("Asus Generic WMI Driver");
Yong Wangee027e42010-03-21 10:26:34 +080045MODULE_LICENSE("GPL");
46
Corentin Charye12e6d92011-02-26 10:20:31 +010047#define to_asus_wmi_driver(pdrv) \
48 (container_of((pdrv), struct asus_wmi_driver, platform_driver))
Yong Wangee027e42010-03-21 10:26:34 +080049
Corentin Charye12e6d92011-02-26 10:20:31 +010050#define ASUS_WMI_MGMT_GUID "97845ED0-4E6D-11DE-8A39-0800200C9A66"
Yong Wangee027e42010-03-21 10:26:34 +080051
Corentin Chary33e0e6f2011-02-06 13:28:34 +010052#define NOTIFY_BRNUP_MIN 0x11
53#define NOTIFY_BRNUP_MAX 0x1f
54#define NOTIFY_BRNDOWN_MIN 0x20
55#define NOTIFY_BRNDOWN_MAX 0x2e
Chris Chiu487579b2019-04-18 14:46:48 +080056#define NOTIFY_FNLOCK_TOGGLE 0x4e
Corentin Charye9809c02011-07-01 11:34:31 +020057#define NOTIFY_KBD_BRTUP 0xc4
58#define NOTIFY_KBD_BRTDWN 0xc5
Chris Chiued99d292018-06-20 22:46:45 +080059#define NOTIFY_KBD_BRTTOGGLE 0xc7
Yurii Pavlovskyib096f622019-05-14 21:07:05 +020060#define NOTIFY_KBD_FBM 0x99
Yong Wangee027e42010-03-21 10:26:34 +080061
Chris Chiu487579b2019-04-18 14:46:48 +080062#define ASUS_WMI_FNLOCK_BIOS_DISABLED BIT(0)
63
Kast Bernd53e755c2015-05-13 16:24:16 +020064#define ASUS_FAN_DESC "cpu_fan"
65#define ASUS_FAN_MFUN 0x13
66#define ASUS_FAN_SFUN_READ 0x06
67#define ASUS_FAN_SFUN_WRITE 0x07
Daniel Drake2889ffc2019-07-29 16:27:37 +080068
69/* Based on standard hwmon pwmX_enable values */
Daniel Drakee3168b82019-07-29 16:27:39 +080070#define ASUS_FAN_CTRL_FULLSPEED 0
Kast Bernd53e755c2015-05-13 16:24:16 +020071#define ASUS_FAN_CTRL_MANUAL 1
72#define ASUS_FAN_CTRL_AUTO 2
73
Daniel Drake9af93db2019-07-17 13:10:58 +080074#define ASUS_FAN_BOOST_MODE_NORMAL 0
75#define ASUS_FAN_BOOST_MODE_OVERBOOST 1
76#define ASUS_FAN_BOOST_MODE_OVERBOOST_MASK 0x01
77#define ASUS_FAN_BOOST_MODE_SILENT 2
78#define ASUS_FAN_BOOST_MODE_SILENT_MASK 0x02
79#define ASUS_FAN_BOOST_MODES_MASK 0x03
Yurii Pavlovskyib096f622019-05-14 21:07:05 +020080
Kai-Chuan Hsieh8023eff2016-09-01 23:55:55 +080081#define USB_INTEL_XUSB2PR 0xD0
82#define PCI_DEVICE_ID_INTEL_LYNXPOINT_LP_XHCI 0x9c31
83
Yurii Pavlovskyie0668f22019-05-14 21:00:31 +020084#define ASUS_ACPI_UID_ASUSWMI "ASUSWMI"
Yurii Pavlovskyi1a373d12019-05-14 21:02:09 +020085#define ASUS_ACPI_UID_ATK "ATK"
86
87#define WMI_EVENT_QUEUE_SIZE 0x10
88#define WMI_EVENT_QUEUE_END 0x1
89#define WMI_EVENT_MASK 0xFFFF
90/* The WMI hotkey event value is always the same. */
91#define WMI_EVENT_VALUE_ATK 0xFF
Yurii Pavlovskyie0668f22019-05-14 21:00:31 +020092
Yurii Pavlovskyi8abd752b2019-05-14 21:01:24 +020093#define WMI_EVENT_MASK 0xFFFF
94
João Paulo Rechi Vita71050ae2017-02-20 14:50:22 -050095static const char * const ashs_ids[] = { "ATK4001", "ATK4002", NULL };
96
João Paulo Rechi Vitacf48bf92018-05-22 14:30:15 -070097static bool ashs_present(void)
98{
99 int i = 0;
100 while (ashs_ids[i]) {
101 if (acpi_dev_found(ashs_ids[i++]))
102 return true;
103 }
104 return false;
105}
106
Yong Wang3d7b1652010-04-11 09:27:54 +0800107struct bios_args {
Corentin Charyd33da3b2011-02-26 10:20:35 +0100108 u32 arg0;
109 u32 arg1;
Yurii Pavlovskyi98e865a2019-05-14 20:54:50 +0200110 u32 arg2; /* At least TUF Gaming series uses 3 dword input buffer. */
Corentin Charyd33da3b2011-02-26 10:20:35 +0100111} __packed;
Yong Wang3d7b1652010-04-11 09:27:54 +0800112
Corentin Chary8c1b2d82010-11-29 08:14:09 +0100113/*
Kast Bernd53e755c2015-05-13 16:24:16 +0200114 * Struct that's used for all methods called via AGFN. Naming is
115 * identically to the AML code.
116 */
117struct agfn_args {
118 u16 mfun; /* probably "Multi-function" to be called */
119 u16 sfun; /* probably "Sub-function" to be called */
120 u16 len; /* size of the hole struct, including subfunction fields */
121 u8 stas; /* not used by now */
122 u8 err; /* zero on success */
123} __packed;
124
125/* struct used for calling fan read and write methods */
Daniel Drake2889ffc2019-07-29 16:27:37 +0800126struct agfn_fan_args {
Kast Bernd53e755c2015-05-13 16:24:16 +0200127 struct agfn_args agfn; /* common fields */
128 u8 fan; /* fan number: 0: set auto mode 1: 1st fan */
129 u32 speed; /* read: RPM/100 - write: 0-255 */
130} __packed;
131
132/*
Corentin Charye12e6d92011-02-26 10:20:31 +0100133 * <platform>/ - debugfs root directory
Corentin Chary8c1b2d82010-11-29 08:14:09 +0100134 * dev_id - current dev_id
135 * ctrl_param - current ctrl_param
Corentin Charyef343492011-02-26 10:20:39 +0100136 * method_id - current method_id
Corentin Chary8c1b2d82010-11-29 08:14:09 +0100137 * devs - call DEVS(dev_id, ctrl_param) and print result
138 * dsts - call DSTS(dev_id) and print result
Corentin Charyef343492011-02-26 10:20:39 +0100139 * call - call method_id(dev_id, ctrl_param) and print result
Corentin Chary8c1b2d82010-11-29 08:14:09 +0100140 */
Corentin Charye12e6d92011-02-26 10:20:31 +0100141struct asus_wmi_debug {
Corentin Chary8c1b2d82010-11-29 08:14:09 +0100142 struct dentry *root;
Corentin Charyef343492011-02-26 10:20:39 +0100143 u32 method_id;
Corentin Chary8c1b2d82010-11-29 08:14:09 +0100144 u32 dev_id;
145 u32 ctrl_param;
146};
147
Corentin Charya7ce3f02011-02-26 10:20:33 +0100148struct asus_rfkill {
149 struct asus_wmi *asus;
150 struct rfkill *rfkill;
151 u32 dev_id;
152};
153
Daniel Drake2889ffc2019-07-29 16:27:37 +0800154enum fan_type {
155 FAN_TYPE_NONE = 0,
156 FAN_TYPE_AGFN, /* deprecated on newer platforms */
Daniel Drakee3168b82019-07-29 16:27:39 +0800157 FAN_TYPE_SPEC83, /* starting in Spec 8.3, use CPU_FAN_CTRL */
Daniel Drake2889ffc2019-07-29 16:27:37 +0800158};
159
Corentin Charye12e6d92011-02-26 10:20:31 +0100160struct asus_wmi {
Corentin Chary1d070f82011-02-26 10:20:36 +0100161 int dsts_id;
Corentin Chary46dbca82011-02-26 10:20:38 +0100162 int spec;
163 int sfun;
Yurii Pavlovskyi1a373d12019-05-14 21:02:09 +0200164 bool wmi_event_queue;
Corentin Chary1d070f82011-02-26 10:20:36 +0100165
Yong Wang81248882010-04-11 09:26:33 +0800166 struct input_dev *inputdev;
Yong Wang3d7b1652010-04-11 09:27:54 +0800167 struct backlight_device *backlight_device;
Corentin Chary27c136c2010-11-29 08:14:05 +0100168 struct platform_device *platform_device;
Corentin Chary084fca62010-11-29 08:14:06 +0100169
AceLan Kao6cae06e2012-07-27 16:51:59 +0800170 struct led_classdev wlan_led;
171 int wlan_led_wk;
Corentin Chary084fca62010-11-29 08:14:06 +0100172 struct led_classdev tpd_led;
173 int tpd_led_wk;
Corentin Charye9809c02011-07-01 11:34:31 +0200174 struct led_classdev kbd_led;
175 int kbd_led_wk;
Maxime Bellengé4c059842017-09-23 18:23:37 +0200176 struct led_classdev lightbar_led;
177 int lightbar_led_wk;
Corentin Chary084fca62010-11-29 08:14:06 +0100178 struct workqueue_struct *led_workqueue;
179 struct work_struct tpd_led_work;
AceLan Kao6cae06e2012-07-27 16:51:59 +0800180 struct work_struct wlan_led_work;
Maxime Bellengé4c059842017-09-23 18:23:37 +0200181 struct work_struct lightbar_led_work;
Corentin Charyba48fdb2010-11-29 08:14:07 +0100182
Corentin Charya7ce3f02011-02-26 10:20:33 +0100183 struct asus_rfkill wlan;
184 struct asus_rfkill bluetooth;
185 struct asus_rfkill wimax;
186 struct asus_rfkill wwan3g;
Corentin Chary43be8bd2011-07-01 11:34:40 +0200187 struct asus_rfkill gps;
Corentin Charya912d322011-07-01 11:34:41 +0200188 struct asus_rfkill uwb;
Corentin Chary8c1b2d82010-11-29 08:14:09 +0100189
Daniel Drake2889ffc2019-07-29 16:27:37 +0800190 enum fan_type fan_type;
191 int fan_pwm_mode;
192 int agfn_pwm;
Kast Bernd53e755c2015-05-13 16:24:16 +0200193
Daniel Drake9af93db2019-07-17 13:10:58 +0800194 bool fan_boost_mode_available;
195 u8 fan_boost_mode_mask;
196 u8 fan_boost_mode;
Yurii Pavlovskyib096f622019-05-14 21:07:05 +0200197
Kristian Klausend507a542019-08-05 21:23:05 +0200198 int charge_threshold;
199
Lukas Wunner125450f2018-09-08 09:59:01 +0200200 struct hotplug_slot hotplug_slot;
Corentin Charyafa7c882011-02-06 13:28:28 +0100201 struct mutex hotplug_lock;
Corentin Chary279f8f92011-02-06 13:28:29 +0100202 struct mutex wmi_lock;
203 struct workqueue_struct *hotplug_workqueue;
204 struct work_struct hotplug_work;
Corentin Charyafa7c882011-02-06 13:28:28 +0100205
Chris Chiu487579b2019-04-18 14:46:48 +0800206 bool fnlock_locked;
207
Corentin Charye12e6d92011-02-26 10:20:31 +0100208 struct asus_wmi_debug debug;
209
210 struct asus_wmi_driver *driver;
Yong Wang81248882010-04-11 09:26:33 +0800211};
212
Yurii Pavlovskyi54a31212019-05-14 21:04:48 +0200213/* Input **********************************************************************/
214
Corentin Charye12e6d92011-02-26 10:20:31 +0100215static int asus_wmi_input_init(struct asus_wmi *asus)
Yong Wangee027e42010-03-21 10:26:34 +0800216{
217 int err;
218
Corentin Charye12e6d92011-02-26 10:20:31 +0100219 asus->inputdev = input_allocate_device();
220 if (!asus->inputdev)
Yong Wangee027e42010-03-21 10:26:34 +0800221 return -ENOMEM;
222
Corentin Chary58a9f392011-03-30 16:32:32 +0200223 asus->inputdev->name = asus->driver->input_name;
224 asus->inputdev->phys = asus->driver->input_phys;
Corentin Charye12e6d92011-02-26 10:20:31 +0100225 asus->inputdev->id.bustype = BUS_HOST;
226 asus->inputdev->dev.parent = &asus->platform_device->dev;
Seth Forshee39bbde02011-07-04 09:49:20 +0200227 set_bit(EV_REP, asus->inputdev->evbit);
Yong Wangee027e42010-03-21 10:26:34 +0800228
Corentin Charye12e6d92011-02-26 10:20:31 +0100229 err = sparse_keymap_setup(asus->inputdev, asus->driver->keymap, NULL);
Yong Wangee027e42010-03-21 10:26:34 +0800230 if (err)
231 goto err_free_dev;
232
Corentin Charye12e6d92011-02-26 10:20:31 +0100233 err = input_register_device(asus->inputdev);
Yong Wangee027e42010-03-21 10:26:34 +0800234 if (err)
Michał Kępieńc2ef3a12017-03-09 13:11:39 +0100235 goto err_free_dev;
Yong Wangee027e42010-03-21 10:26:34 +0800236
237 return 0;
238
Yong Wangee027e42010-03-21 10:26:34 +0800239err_free_dev:
Corentin Charye12e6d92011-02-26 10:20:31 +0100240 input_free_device(asus->inputdev);
Yong Wangee027e42010-03-21 10:26:34 +0800241 return err;
242}
243
Corentin Charye12e6d92011-02-26 10:20:31 +0100244static void asus_wmi_input_exit(struct asus_wmi *asus)
Yong Wang81248882010-04-11 09:26:33 +0800245{
Michał Kępieńc2ef3a12017-03-09 13:11:39 +0100246 if (asus->inputdev)
Corentin Charye12e6d92011-02-26 10:20:31 +0100247 input_unregister_device(asus->inputdev);
Yong Wang81248882010-04-11 09:26:33 +0800248
Corentin Charye12e6d92011-02-26 10:20:31 +0100249 asus->inputdev = NULL;
Yong Wang81248882010-04-11 09:26:33 +0800250}
251
Yurii Pavlovskyi54a31212019-05-14 21:04:48 +0200252/* WMI ************************************************************************/
253
Yurii Pavlovskyi98e865a2019-05-14 20:54:50 +0200254static int asus_wmi_evaluate_method3(u32 method_id,
255 u32 arg0, u32 arg1, u32 arg2, u32 *retval)
Yong Wang3d7b1652010-04-11 09:27:54 +0800256{
Corentin Charyd33da3b2011-02-26 10:20:35 +0100257 struct bios_args args = {
258 .arg0 = arg0,
259 .arg1 = arg1,
Yurii Pavlovskyi98e865a2019-05-14 20:54:50 +0200260 .arg2 = arg2,
Corentin Charyd33da3b2011-02-26 10:20:35 +0100261 };
262 struct acpi_buffer input = { (acpi_size) sizeof(args), &args };
Yong Wang3d7b1652010-04-11 09:27:54 +0800263 struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER, NULL };
Yong Wang3d7b1652010-04-11 09:27:54 +0800264 acpi_status status;
Corentin Charyd33da3b2011-02-26 10:20:35 +0100265 union acpi_object *obj;
Rickard Strandqvist8ad3be12014-06-01 14:58:52 +0200266 u32 tmp = 0;
Yong Wang3d7b1652010-04-11 09:27:54 +0800267
Pali Rohár0fe57262017-08-12 09:44:16 +0200268 status = wmi_evaluate_method(ASUS_WMI_MGMT_GUID, 0, method_id,
Corentin Charyafa7c882011-02-06 13:28:28 +0100269 &input, &output);
Yong Wang3d7b1652010-04-11 09:27:54 +0800270
271 if (ACPI_FAILURE(status))
Yurii Pavlovskyi1827f3f2019-05-14 21:03:50 +0200272 return -EIO;
Yong Wang3d7b1652010-04-11 09:27:54 +0800273
274 obj = (union acpi_object *)output.pointer;
275 if (obj && obj->type == ACPI_TYPE_INTEGER)
Corentin Charye12e6d92011-02-26 10:20:31 +0100276 tmp = (u32) obj->integer.value;
Yong Wang3d7b1652010-04-11 09:27:54 +0800277
Corentin Chary2a3f0062010-11-29 08:14:10 +0100278 if (retval)
279 *retval = tmp;
Yong Wang3d7b1652010-04-11 09:27:54 +0800280
281 kfree(obj);
282
Corentin Charyd33da3b2011-02-26 10:20:35 +0100283 if (tmp == ASUS_WMI_UNSUPPORTED_METHOD)
284 return -ENODEV;
285
286 return 0;
Yong Wang3d7b1652010-04-11 09:27:54 +0800287}
Yurii Pavlovskyi98e865a2019-05-14 20:54:50 +0200288
289int asus_wmi_evaluate_method(u32 method_id, u32 arg0, u32 arg1, u32 *retval)
290{
291 return asus_wmi_evaluate_method3(method_id, arg0, arg1, 0, retval);
292}
Daniel Drakeffb6ce72018-10-09 14:40:55 +0800293EXPORT_SYMBOL_GPL(asus_wmi_evaluate_method);
Yong Wang3d7b1652010-04-11 09:27:54 +0800294
Kast Bernd53e755c2015-05-13 16:24:16 +0200295static int asus_wmi_evaluate_method_agfn(const struct acpi_buffer args)
296{
297 struct acpi_buffer input;
298 u64 phys_addr;
299 u32 retval;
300 u32 status = -1;
301
302 /*
303 * Copy to dma capable address otherwise memory corruption occurs as
304 * bios has to be able to access it.
305 */
Fuqian Huang340f25f2019-07-04 00:29:51 +0800306 input.pointer = kmemdup(args.pointer, args.length, GFP_DMA | GFP_KERNEL);
Kast Bernd53e755c2015-05-13 16:24:16 +0200307 input.length = args.length;
308 if (!input.pointer)
309 return -ENOMEM;
310 phys_addr = virt_to_phys(input.pointer);
Kast Bernd53e755c2015-05-13 16:24:16 +0200311
312 status = asus_wmi_evaluate_method(ASUS_WMI_METHODID_AGFN,
313 phys_addr, 0, &retval);
314 if (!status)
315 memcpy(args.pointer, input.pointer, args.length);
316
317 kfree(input.pointer);
318 if (status)
319 return -ENXIO;
320
321 return retval;
322}
323
Corentin Chary1d070f82011-02-26 10:20:36 +0100324static int asus_wmi_get_devstate(struct asus_wmi *asus, u32 dev_id, u32 *retval)
Corentin Charyd33da3b2011-02-26 10:20:35 +0100325{
Corentin Chary1d070f82011-02-26 10:20:36 +0100326 return asus_wmi_evaluate_method(asus->dsts_id, dev_id, 0, retval);
Corentin Charyd33da3b2011-02-26 10:20:35 +0100327}
328
329static int asus_wmi_set_devstate(u32 dev_id, u32 ctrl_param,
Corentin Chary1d070f82011-02-26 10:20:36 +0100330 u32 *retval)
Yong Wang3d7b1652010-04-11 09:27:54 +0800331{
Corentin Charyd33da3b2011-02-26 10:20:35 +0100332 return asus_wmi_evaluate_method(ASUS_WMI_METHODID_DEVS, dev_id,
333 ctrl_param, retval);
Yong Wang3d7b1652010-04-11 09:27:54 +0800334}
335
Corentin Chary5c956382011-02-06 13:28:31 +0100336/* Helper for special devices with magic return codes */
Corentin Chary1d070f82011-02-26 10:20:36 +0100337static int asus_wmi_get_devstate_bits(struct asus_wmi *asus,
338 u32 dev_id, u32 mask)
Corentin Chary5c956382011-02-06 13:28:31 +0100339{
340 u32 retval = 0;
Corentin Charyd33da3b2011-02-26 10:20:35 +0100341 int err;
Corentin Chary5c956382011-02-06 13:28:31 +0100342
Corentin Chary1d070f82011-02-26 10:20:36 +0100343 err = asus_wmi_get_devstate(asus, dev_id, &retval);
Corentin Chary5c956382011-02-06 13:28:31 +0100344
Corentin Charyd33da3b2011-02-26 10:20:35 +0100345 if (err < 0)
346 return err;
Corentin Chary5c956382011-02-06 13:28:31 +0100347
Corentin Charye12e6d92011-02-26 10:20:31 +0100348 if (!(retval & ASUS_WMI_DSTS_PRESENCE_BIT))
Corentin Chary5c956382011-02-06 13:28:31 +0100349 return -ENODEV;
350
Corentin Charya75fe0d2011-02-26 10:20:34 +0100351 if (mask == ASUS_WMI_DSTS_STATUS_BIT) {
352 if (retval & ASUS_WMI_DSTS_UNKNOWN_BIT)
353 return -ENODEV;
354 }
355
Corentin Charyb7187262011-02-06 13:28:39 +0100356 return retval & mask;
357}
358
Corentin Chary1d070f82011-02-26 10:20:36 +0100359static int asus_wmi_get_devstate_simple(struct asus_wmi *asus, u32 dev_id)
Corentin Charyb7187262011-02-06 13:28:39 +0100360{
Corentin Chary1d070f82011-02-26 10:20:36 +0100361 return asus_wmi_get_devstate_bits(asus, dev_id,
362 ASUS_WMI_DSTS_STATUS_BIT);
Corentin Chary5c956382011-02-06 13:28:31 +0100363}
364
Daniel Drakef1fc0322019-07-29 16:27:38 +0800365static bool asus_wmi_dev_is_present(struct asus_wmi *asus, u32 dev_id)
366{
367 u32 retval;
368 int status = asus_wmi_get_devstate(asus, dev_id, &retval);
369
370 return status == 0 && (retval & ASUS_WMI_DSTS_PRESENCE_BIT);
371}
372
Yurii Pavlovskyi54a31212019-05-14 21:04:48 +0200373/* LEDs ***********************************************************************/
374
Corentin Chary084fca62010-11-29 08:14:06 +0100375/*
376 * These functions actually update the LED's, and are called from a
377 * workqueue. By doing this as separate work rather than when the LED
Corentin Charye12e6d92011-02-26 10:20:31 +0100378 * subsystem asks, we avoid messing with the Asus ACPI stuff during a
Corentin Chary084fca62010-11-29 08:14:06 +0100379 * potentially bad time, such as a timer interrupt.
380 */
381static void tpd_led_update(struct work_struct *work)
382{
383 int ctrl_param;
Corentin Charye12e6d92011-02-26 10:20:31 +0100384 struct asus_wmi *asus;
Corentin Chary084fca62010-11-29 08:14:06 +0100385
Corentin Charye12e6d92011-02-26 10:20:31 +0100386 asus = container_of(work, struct asus_wmi, tpd_led_work);
Corentin Chary084fca62010-11-29 08:14:06 +0100387
Corentin Charye12e6d92011-02-26 10:20:31 +0100388 ctrl_param = asus->tpd_led_wk;
389 asus_wmi_set_devstate(ASUS_WMI_DEVID_TOUCHPAD_LED, ctrl_param, NULL);
Corentin Chary084fca62010-11-29 08:14:06 +0100390}
391
392static void tpd_led_set(struct led_classdev *led_cdev,
393 enum led_brightness value)
394{
Corentin Charye12e6d92011-02-26 10:20:31 +0100395 struct asus_wmi *asus;
Corentin Chary084fca62010-11-29 08:14:06 +0100396
Corentin Charye12e6d92011-02-26 10:20:31 +0100397 asus = container_of(led_cdev, struct asus_wmi, tpd_led);
Corentin Chary084fca62010-11-29 08:14:06 +0100398
Corentin Charye12e6d92011-02-26 10:20:31 +0100399 asus->tpd_led_wk = !!value;
400 queue_work(asus->led_workqueue, &asus->tpd_led_work);
Corentin Chary084fca62010-11-29 08:14:06 +0100401}
402
Corentin Charye12e6d92011-02-26 10:20:31 +0100403static int read_tpd_led_state(struct asus_wmi *asus)
Corentin Chary084fca62010-11-29 08:14:06 +0100404{
Corentin Chary1d070f82011-02-26 10:20:36 +0100405 return asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_TOUCHPAD_LED);
Corentin Chary084fca62010-11-29 08:14:06 +0100406}
407
408static enum led_brightness tpd_led_get(struct led_classdev *led_cdev)
409{
Corentin Charye12e6d92011-02-26 10:20:31 +0100410 struct asus_wmi *asus;
Corentin Chary084fca62010-11-29 08:14:06 +0100411
Corentin Charye12e6d92011-02-26 10:20:31 +0100412 asus = container_of(led_cdev, struct asus_wmi, tpd_led);
Corentin Chary084fca62010-11-29 08:14:06 +0100413
Corentin Charye12e6d92011-02-26 10:20:31 +0100414 return read_tpd_led_state(asus);
Corentin Chary084fca62010-11-29 08:14:06 +0100415}
416
Jian-Hong Pan9fe44fc2018-09-27 16:50:09 +0800417static void kbd_led_update(struct asus_wmi *asus)
Corentin Chary084fca62010-11-29 08:14:06 +0100418{
Corentin Charye9809c02011-07-01 11:34:31 +0200419 int ctrl_param = 0;
Corentin Chary084fca62010-11-29 08:14:06 +0100420
Corentin Charye9809c02011-07-01 11:34:31 +0200421 /*
422 * bits 0-2: level
423 * bit 7: light on/off
424 */
425 if (asus->kbd_led_wk > 0)
426 ctrl_param = 0x80 | (asus->kbd_led_wk & 0x7F);
Corentin Chary084fca62010-11-29 08:14:06 +0100427
Corentin Charye9809c02011-07-01 11:34:31 +0200428 asus_wmi_set_devstate(ASUS_WMI_DEVID_KBD_BACKLIGHT, ctrl_param, NULL);
429}
Corentin Chary084fca62010-11-29 08:14:06 +0100430
Corentin Charye9809c02011-07-01 11:34:31 +0200431static int kbd_led_read(struct asus_wmi *asus, int *level, int *env)
432{
433 int retval;
434
435 /*
436 * bits 0-2: level
437 * bit 7: light on/off
438 * bit 8-10: environment (0: dark, 1: normal, 2: light)
439 * bit 17: status unknown
440 */
441 retval = asus_wmi_get_devstate_bits(asus, ASUS_WMI_DEVID_KBD_BACKLIGHT,
442 0xFFFF);
443
Corentin Charyaf965e92011-07-01 11:34:34 +0200444 /* Unknown status is considered as off */
Corentin Charye9809c02011-07-01 11:34:31 +0200445 if (retval == 0x8000)
Corentin Charyaf965e92011-07-01 11:34:34 +0200446 retval = 0;
Corentin Charye9809c02011-07-01 11:34:31 +0200447
448 if (retval >= 0) {
449 if (level)
Corentin Charyc09b2232012-03-20 09:53:04 +0100450 *level = retval & 0x7F;
Corentin Charye9809c02011-07-01 11:34:31 +0200451 if (env)
452 *env = (retval >> 8) & 0x7F;
453 retval = 0;
Corentin Chary084fca62010-11-29 08:14:06 +0100454 }
455
Corentin Charye9809c02011-07-01 11:34:31 +0200456 return retval;
457}
458
Chris Chiudbb3d782018-06-20 22:46:44 +0800459static void do_kbd_led_set(struct led_classdev *led_cdev, int value)
Corentin Charye9809c02011-07-01 11:34:31 +0200460{
461 struct asus_wmi *asus;
Chris Chiudbb3d782018-06-20 22:46:44 +0800462 int max_level;
Corentin Charye9809c02011-07-01 11:34:31 +0200463
464 asus = container_of(led_cdev, struct asus_wmi, kbd_led);
Chris Chiudbb3d782018-06-20 22:46:44 +0800465 max_level = asus->kbd_led.max_brightness;
Corentin Charye9809c02011-07-01 11:34:31 +0200466
Chris Chiudbb3d782018-06-20 22:46:44 +0800467 if (value > max_level)
468 value = max_level;
Corentin Charye9809c02011-07-01 11:34:31 +0200469 else if (value < 0)
470 value = 0;
471
472 asus->kbd_led_wk = value;
Jian-Hong Pan9fe44fc2018-09-27 16:50:09 +0800473 kbd_led_update(asus);
Corentin Charye9809c02011-07-01 11:34:31 +0200474}
475
Chris Chiudbb3d782018-06-20 22:46:44 +0800476static void kbd_led_set(struct led_classdev *led_cdev,
477 enum led_brightness value)
478{
Yurii Pavlovskyi3e581672019-05-14 21:07:46 +0200479 /* Prevent disabling keyboard backlight on module unregister */
480 if (led_cdev->flags & LED_UNREGISTERING)
481 return;
482
Chris Chiudbb3d782018-06-20 22:46:44 +0800483 do_kbd_led_set(led_cdev, value);
484}
485
Jian-Hong Pan29f6eb52018-10-22 18:00:04 +0800486static void kbd_led_set_by_kbd(struct asus_wmi *asus, enum led_brightness value)
487{
488 struct led_classdev *led_cdev = &asus->kbd_led;
489
490 do_kbd_led_set(led_cdev, value);
491 led_classdev_notify_brightness_hw_changed(led_cdev, asus->kbd_led_wk);
492}
493
Corentin Charye9809c02011-07-01 11:34:31 +0200494static enum led_brightness kbd_led_get(struct led_classdev *led_cdev)
495{
496 struct asus_wmi *asus;
497 int retval, value;
498
499 asus = container_of(led_cdev, struct asus_wmi, kbd_led);
500
501 retval = kbd_led_read(asus, &value, NULL);
502
503 if (retval < 0)
504 return retval;
505
506 return value;
Corentin Chary084fca62010-11-29 08:14:06 +0100507}
508
AceLan Kao6cae06e2012-07-27 16:51:59 +0800509static int wlan_led_unknown_state(struct asus_wmi *asus)
510{
511 u32 result;
512
513 asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_WIRELESS_LED, &result);
514
515 return result & ASUS_WMI_DSTS_UNKNOWN_BIT;
516}
517
AceLan Kao6cae06e2012-07-27 16:51:59 +0800518static void wlan_led_update(struct work_struct *work)
519{
520 int ctrl_param;
521 struct asus_wmi *asus;
522
523 asus = container_of(work, struct asus_wmi, wlan_led_work);
524
525 ctrl_param = asus->wlan_led_wk;
526 asus_wmi_set_devstate(ASUS_WMI_DEVID_WIRELESS_LED, ctrl_param, NULL);
527}
528
529static void wlan_led_set(struct led_classdev *led_cdev,
530 enum led_brightness value)
531{
532 struct asus_wmi *asus;
533
534 asus = container_of(led_cdev, struct asus_wmi, wlan_led);
535
536 asus->wlan_led_wk = !!value;
537 queue_work(asus->led_workqueue, &asus->wlan_led_work);
538}
539
540static enum led_brightness wlan_led_get(struct led_classdev *led_cdev)
541{
542 struct asus_wmi *asus;
543 u32 result;
544
545 asus = container_of(led_cdev, struct asus_wmi, wlan_led);
546 asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_WIRELESS_LED, &result);
547
548 return result & ASUS_WMI_DSTS_BRIGHTNESS_MASK;
549}
550
Maxime Bellengé4c059842017-09-23 18:23:37 +0200551static void lightbar_led_update(struct work_struct *work)
552{
553 struct asus_wmi *asus;
554 int ctrl_param;
555
556 asus = container_of(work, struct asus_wmi, lightbar_led_work);
557
558 ctrl_param = asus->lightbar_led_wk;
559 asus_wmi_set_devstate(ASUS_WMI_DEVID_LIGHTBAR, ctrl_param, NULL);
560}
561
562static void lightbar_led_set(struct led_classdev *led_cdev,
563 enum led_brightness value)
564{
565 struct asus_wmi *asus;
566
567 asus = container_of(led_cdev, struct asus_wmi, lightbar_led);
568
569 asus->lightbar_led_wk = !!value;
570 queue_work(asus->led_workqueue, &asus->lightbar_led_work);
571}
572
573static enum led_brightness lightbar_led_get(struct led_classdev *led_cdev)
574{
575 struct asus_wmi *asus;
576 u32 result;
577
578 asus = container_of(led_cdev, struct asus_wmi, lightbar_led);
579 asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_LIGHTBAR, &result);
580
581 return result & ASUS_WMI_DSTS_LIGHTBAR_MASK;
582}
583
Corentin Charye12e6d92011-02-26 10:20:31 +0100584static void asus_wmi_led_exit(struct asus_wmi *asus)
Corentin Chary084fca62010-11-29 08:14:06 +0100585{
Axel Line9298022011-08-08 17:16:01 +0800586 if (!IS_ERR_OR_NULL(asus->kbd_led.dev))
587 led_classdev_unregister(&asus->kbd_led);
588 if (!IS_ERR_OR_NULL(asus->tpd_led.dev))
Corentin Charye12e6d92011-02-26 10:20:31 +0100589 led_classdev_unregister(&asus->tpd_led);
AceLan Kao6cae06e2012-07-27 16:51:59 +0800590 if (!IS_ERR_OR_NULL(asus->wlan_led.dev))
591 led_classdev_unregister(&asus->wlan_led);
Maxime Bellengé4c059842017-09-23 18:23:37 +0200592 if (!IS_ERR_OR_NULL(asus->lightbar_led.dev))
593 led_classdev_unregister(&asus->lightbar_led);
Corentin Charye12e6d92011-02-26 10:20:31 +0100594 if (asus->led_workqueue)
595 destroy_workqueue(asus->led_workqueue);
Corentin Chary084fca62010-11-29 08:14:06 +0100596}
597
Corentin Charye9809c02011-07-01 11:34:31 +0200598static int asus_wmi_led_init(struct asus_wmi *asus)
599{
Oleksij Rempel30734042015-09-14 11:16:30 +0200600 int rv = 0, led_val;
Corentin Charye9809c02011-07-01 11:34:31 +0200601
602 asus->led_workqueue = create_singlethread_workqueue("led_workqueue");
603 if (!asus->led_workqueue)
604 return -ENOMEM;
605
606 if (read_tpd_led_state(asus) >= 0) {
607 INIT_WORK(&asus->tpd_led_work, tpd_led_update);
608
609 asus->tpd_led.name = "asus::touchpad";
610 asus->tpd_led.brightness_set = tpd_led_set;
611 asus->tpd_led.brightness_get = tpd_led_get;
612 asus->tpd_led.max_brightness = 1;
613
614 rv = led_classdev_register(&asus->platform_device->dev,
615 &asus->tpd_led);
616 if (rv)
617 goto error;
618 }
619
Yurii Pavlovskyi8853a2f2019-05-14 20:51:25 +0200620 if (!kbd_led_read(asus, &led_val, NULL)) {
Oleksij Rempel30734042015-09-14 11:16:30 +0200621 asus->kbd_led_wk = led_val;
Corentin Charye9809c02011-07-01 11:34:31 +0200622 asus->kbd_led.name = "asus::kbd_backlight";
Chris Chiudbb3d782018-06-20 22:46:44 +0800623 asus->kbd_led.flags = LED_BRIGHT_HW_CHANGED;
Corentin Charye9809c02011-07-01 11:34:31 +0200624 asus->kbd_led.brightness_set = kbd_led_set;
625 asus->kbd_led.brightness_get = kbd_led_get;
626 asus->kbd_led.max_brightness = 3;
627
628 rv = led_classdev_register(&asus->platform_device->dev,
629 &asus->kbd_led);
AceLan Kao6cae06e2012-07-27 16:51:59 +0800630 if (rv)
631 goto error;
632 }
633
Daniel Drakef1fc0322019-07-29 16:27:38 +0800634 if (asus_wmi_dev_is_present(asus, ASUS_WMI_DEVID_WIRELESS_LED)
635 && (asus->driver->quirks->wapf > 0)) {
AceLan Kao6cae06e2012-07-27 16:51:59 +0800636 INIT_WORK(&asus->wlan_led_work, wlan_led_update);
637
638 asus->wlan_led.name = "asus::wlan";
639 asus->wlan_led.brightness_set = wlan_led_set;
640 if (!wlan_led_unknown_state(asus))
641 asus->wlan_led.brightness_get = wlan_led_get;
642 asus->wlan_led.flags = LED_CORE_SUSPENDRESUME;
643 asus->wlan_led.max_brightness = 1;
644 asus->wlan_led.default_trigger = "asus-wlan";
645
646 rv = led_classdev_register(&asus->platform_device->dev,
647 &asus->wlan_led);
Maxime Bellengé4c059842017-09-23 18:23:37 +0200648 if (rv)
649 goto error;
650 }
651
Daniel Drakef1fc0322019-07-29 16:27:38 +0800652 if (asus_wmi_dev_is_present(asus, ASUS_WMI_DEVID_LIGHTBAR)) {
Maxime Bellengé4c059842017-09-23 18:23:37 +0200653 INIT_WORK(&asus->lightbar_led_work, lightbar_led_update);
654
655 asus->lightbar_led.name = "asus::lightbar";
656 asus->lightbar_led.brightness_set = lightbar_led_set;
657 asus->lightbar_led.brightness_get = lightbar_led_get;
658 asus->lightbar_led.max_brightness = 1;
659
660 rv = led_classdev_register(&asus->platform_device->dev,
661 &asus->lightbar_led);
Corentin Charye9809c02011-07-01 11:34:31 +0200662 }
663
664error:
665 if (rv)
666 asus_wmi_led_exit(asus);
667
668 return rv;
669}
670
Yurii Pavlovskyi54a31212019-05-14 21:04:48 +0200671/* RF *************************************************************************/
Corentin Charye9809c02011-07-01 11:34:31 +0200672
Corentin Chary084fca62010-11-29 08:14:06 +0100673/*
Corentin Charyafa7c882011-02-06 13:28:28 +0100674 * PCI hotplug (for wlan rfkill)
675 */
Corentin Charye12e6d92011-02-26 10:20:31 +0100676static bool asus_wlan_rfkill_blocked(struct asus_wmi *asus)
Corentin Charyafa7c882011-02-06 13:28:28 +0100677{
Corentin Chary1d070f82011-02-26 10:20:36 +0100678 int result = asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_WLAN);
Corentin Charyafa7c882011-02-06 13:28:28 +0100679
Corentin Chary5c956382011-02-06 13:28:31 +0100680 if (result < 0)
Corentin Charyafa7c882011-02-06 13:28:28 +0100681 return false;
Corentin Chary5c956382011-02-06 13:28:31 +0100682 return !result;
Corentin Charyafa7c882011-02-06 13:28:28 +0100683}
684
Corentin Charye12e6d92011-02-26 10:20:31 +0100685static void asus_rfkill_hotplug(struct asus_wmi *asus)
Corentin Charyafa7c882011-02-06 13:28:28 +0100686{
687 struct pci_dev *dev;
688 struct pci_bus *bus;
Corentin Chary279f8f92011-02-06 13:28:29 +0100689 bool blocked;
Corentin Charyafa7c882011-02-06 13:28:28 +0100690 bool absent;
691 u32 l;
692
Corentin Charye12e6d92011-02-26 10:20:31 +0100693 mutex_lock(&asus->wmi_lock);
694 blocked = asus_wlan_rfkill_blocked(asus);
695 mutex_unlock(&asus->wmi_lock);
Corentin Charyafa7c882011-02-06 13:28:28 +0100696
Corentin Charye12e6d92011-02-26 10:20:31 +0100697 mutex_lock(&asus->hotplug_lock);
Rafael J. Wysocki8b9ec1d2014-01-10 15:27:08 +0100698 pci_lock_rescan_remove();
Corentin Charyafa7c882011-02-06 13:28:28 +0100699
Corentin Charya7ce3f02011-02-26 10:20:33 +0100700 if (asus->wlan.rfkill)
701 rfkill_set_sw_state(asus->wlan.rfkill, blocked);
Corentin Chary279f8f92011-02-06 13:28:29 +0100702
Lukas Wunner125450f2018-09-08 09:59:01 +0200703 if (asus->hotplug_slot.ops) {
Corentin Charyafa7c882011-02-06 13:28:28 +0100704 bus = pci_find_bus(0, 1);
705 if (!bus) {
Joe Perches5ad77dc2011-03-29 15:21:35 -0700706 pr_warn("Unable to find PCI bus 1?\n");
Corentin Charyafa7c882011-02-06 13:28:28 +0100707 goto out_unlock;
708 }
709
710 if (pci_bus_read_config_dword(bus, 0, PCI_VENDOR_ID, &l)) {
711 pr_err("Unable to read PCI config space?\n");
712 goto out_unlock;
713 }
714 absent = (l == 0xffffffff);
715
716 if (blocked != absent) {
Joe Perches5ad77dc2011-03-29 15:21:35 -0700717 pr_warn("BIOS says wireless lan is %s, "
718 "but the pci device is %s\n",
719 blocked ? "blocked" : "unblocked",
720 absent ? "absent" : "present");
721 pr_warn("skipped wireless hotplug as probably "
722 "inappropriate for this model\n");
Corentin Charyafa7c882011-02-06 13:28:28 +0100723 goto out_unlock;
724 }
725
726 if (!blocked) {
727 dev = pci_get_slot(bus, 0);
728 if (dev) {
729 /* Device already present */
730 pci_dev_put(dev);
731 goto out_unlock;
732 }
733 dev = pci_scan_single_device(bus, 0);
734 if (dev) {
735 pci_bus_assign_resources(bus);
Yijing Wangc893d132014-05-30 11:01:03 +0800736 pci_bus_add_device(dev);
Corentin Charyafa7c882011-02-06 13:28:28 +0100737 }
738 } else {
739 dev = pci_get_slot(bus, 0);
740 if (dev) {
Yinghai Lu210647a2012-02-25 13:54:20 -0800741 pci_stop_and_remove_bus_device(dev);
Corentin Charyafa7c882011-02-06 13:28:28 +0100742 pci_dev_put(dev);
743 }
744 }
745 }
746
747out_unlock:
Rafael J. Wysocki8b9ec1d2014-01-10 15:27:08 +0100748 pci_unlock_rescan_remove();
Corentin Charye12e6d92011-02-26 10:20:31 +0100749 mutex_unlock(&asus->hotplug_lock);
Corentin Charyafa7c882011-02-06 13:28:28 +0100750}
751
Corentin Charye12e6d92011-02-26 10:20:31 +0100752static void asus_rfkill_notify(acpi_handle handle, u32 event, void *data)
Corentin Charyafa7c882011-02-06 13:28:28 +0100753{
Corentin Charye12e6d92011-02-26 10:20:31 +0100754 struct asus_wmi *asus = data;
Corentin Charyafa7c882011-02-06 13:28:28 +0100755
756 if (event != ACPI_NOTIFY_BUS_CHECK)
757 return;
758
Corentin Chary279f8f92011-02-06 13:28:29 +0100759 /*
Corentin Charye12e6d92011-02-26 10:20:31 +0100760 * We can't call directly asus_rfkill_hotplug because most
Corentin Chary279f8f92011-02-06 13:28:29 +0100761 * of the time WMBC is still being executed and not reetrant.
762 * There is currently no way to tell ACPICA that we want this
Corentin Charye12e6d92011-02-26 10:20:31 +0100763 * method to be serialized, we schedule a asus_rfkill_hotplug
Corentin Chary279f8f92011-02-06 13:28:29 +0100764 * call later, in a safer context.
765 */
Corentin Charye12e6d92011-02-26 10:20:31 +0100766 queue_work(asus->hotplug_workqueue, &asus->hotplug_work);
Corentin Charyafa7c882011-02-06 13:28:28 +0100767}
768
Corentin Charye12e6d92011-02-26 10:20:31 +0100769static int asus_register_rfkill_notifier(struct asus_wmi *asus, char *node)
Corentin Charyafa7c882011-02-06 13:28:28 +0100770{
771 acpi_status status;
772 acpi_handle handle;
773
774 status = acpi_get_handle(NULL, node, &handle);
775
776 if (ACPI_SUCCESS(status)) {
777 status = acpi_install_notify_handler(handle,
778 ACPI_SYSTEM_NOTIFY,
Corentin Charye12e6d92011-02-26 10:20:31 +0100779 asus_rfkill_notify, asus);
Corentin Charyafa7c882011-02-06 13:28:28 +0100780 if (ACPI_FAILURE(status))
Joe Perches5ad77dc2011-03-29 15:21:35 -0700781 pr_warn("Failed to register notify on %s\n", node);
Corentin Charyafa7c882011-02-06 13:28:28 +0100782 } else
783 return -ENODEV;
784
785 return 0;
786}
787
Corentin Charye12e6d92011-02-26 10:20:31 +0100788static void asus_unregister_rfkill_notifier(struct asus_wmi *asus, char *node)
Corentin Charyafa7c882011-02-06 13:28:28 +0100789{
790 acpi_status status = AE_OK;
791 acpi_handle handle;
792
793 status = acpi_get_handle(NULL, node, &handle);
794
795 if (ACPI_SUCCESS(status)) {
796 status = acpi_remove_notify_handler(handle,
Corentin Charye12e6d92011-02-26 10:20:31 +0100797 ACPI_SYSTEM_NOTIFY,
798 asus_rfkill_notify);
Corentin Charyafa7c882011-02-06 13:28:28 +0100799 if (ACPI_FAILURE(status))
800 pr_err("Error removing rfkill notify handler %s\n",
Corentin Charye12e6d92011-02-26 10:20:31 +0100801 node);
Corentin Charyafa7c882011-02-06 13:28:28 +0100802 }
803}
804
Corentin Charye12e6d92011-02-26 10:20:31 +0100805static int asus_get_adapter_status(struct hotplug_slot *hotplug_slot,
806 u8 *value)
Corentin Charyafa7c882011-02-06 13:28:28 +0100807{
Lukas Wunner125450f2018-09-08 09:59:01 +0200808 struct asus_wmi *asus = container_of(hotplug_slot,
809 struct asus_wmi, hotplug_slot);
Corentin Chary1d070f82011-02-26 10:20:36 +0100810 int result = asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_WLAN);
Corentin Charyafa7c882011-02-06 13:28:28 +0100811
Corentin Chary5c956382011-02-06 13:28:31 +0100812 if (result < 0)
813 return result;
Corentin Charyafa7c882011-02-06 13:28:28 +0100814
Corentin Chary5c956382011-02-06 13:28:31 +0100815 *value = !!result;
Corentin Charyafa7c882011-02-06 13:28:28 +0100816 return 0;
817}
818
Lukas Wunner81c4b5b2018-09-08 09:59:01 +0200819static const struct hotplug_slot_ops asus_hotplug_slot_ops = {
Corentin Charye12e6d92011-02-26 10:20:31 +0100820 .get_adapter_status = asus_get_adapter_status,
821 .get_power_status = asus_get_adapter_status,
Corentin Charyafa7c882011-02-06 13:28:28 +0100822};
823
Corentin Charye12e6d92011-02-26 10:20:31 +0100824static void asus_hotplug_work(struct work_struct *work)
Corentin Chary279f8f92011-02-06 13:28:29 +0100825{
Corentin Charye12e6d92011-02-26 10:20:31 +0100826 struct asus_wmi *asus;
Corentin Chary279f8f92011-02-06 13:28:29 +0100827
Corentin Charye12e6d92011-02-26 10:20:31 +0100828 asus = container_of(work, struct asus_wmi, hotplug_work);
829 asus_rfkill_hotplug(asus);
Corentin Chary279f8f92011-02-06 13:28:29 +0100830}
831
Corentin Charye12e6d92011-02-26 10:20:31 +0100832static int asus_setup_pci_hotplug(struct asus_wmi *asus)
Corentin Charyafa7c882011-02-06 13:28:28 +0100833{
834 int ret = -ENOMEM;
835 struct pci_bus *bus = pci_find_bus(0, 1);
836
837 if (!bus) {
838 pr_err("Unable to find wifi PCI bus\n");
839 return -ENODEV;
840 }
841
Corentin Charye12e6d92011-02-26 10:20:31 +0100842 asus->hotplug_workqueue =
843 create_singlethread_workqueue("hotplug_workqueue");
844 if (!asus->hotplug_workqueue)
Corentin Chary279f8f92011-02-06 13:28:29 +0100845 goto error_workqueue;
846
Corentin Charye12e6d92011-02-26 10:20:31 +0100847 INIT_WORK(&asus->hotplug_work, asus_hotplug_work);
Corentin Chary279f8f92011-02-06 13:28:29 +0100848
Lukas Wunner125450f2018-09-08 09:59:01 +0200849 asus->hotplug_slot.ops = &asus_hotplug_slot_ops;
Corentin Charyafa7c882011-02-06 13:28:28 +0100850
Lukas Wunner125450f2018-09-08 09:59:01 +0200851 ret = pci_hp_register(&asus->hotplug_slot, bus, 0, "asus-wifi");
Corentin Charyafa7c882011-02-06 13:28:28 +0100852 if (ret) {
853 pr_err("Unable to register hotplug slot - %d\n", ret);
854 goto error_register;
855 }
856
857 return 0;
858
859error_register:
Lukas Wunner125450f2018-09-08 09:59:01 +0200860 asus->hotplug_slot.ops = NULL;
Corentin Charye12e6d92011-02-26 10:20:31 +0100861 destroy_workqueue(asus->hotplug_workqueue);
Corentin Chary279f8f92011-02-06 13:28:29 +0100862error_workqueue:
Corentin Charyafa7c882011-02-06 13:28:28 +0100863 return ret;
864}
865
866/*
Corentin Charyba48fdb2010-11-29 08:14:07 +0100867 * Rfkill devices
868 */
Corentin Charye12e6d92011-02-26 10:20:31 +0100869static int asus_rfkill_set(void *data, bool blocked)
Corentin Charyba48fdb2010-11-29 08:14:07 +0100870{
Corentin Charya7ce3f02011-02-26 10:20:33 +0100871 struct asus_rfkill *priv = data;
Corentin Charyba48fdb2010-11-29 08:14:07 +0100872 u32 ctrl_param = !blocked;
AceLan Kaoa50bd122012-07-26 17:13:31 +0800873 u32 dev_id = priv->dev_id;
Corentin Charyba48fdb2010-11-29 08:14:07 +0100874
AceLan Kaoa50bd122012-07-26 17:13:31 +0800875 /*
876 * If the user bit is set, BIOS can't set and record the wlan status,
877 * it will report the value read from id ASUS_WMI_DEVID_WLAN_LED
878 * while we query the wlan status through WMI(ASUS_WMI_DEVID_WLAN).
879 * So, we have to record wlan status in id ASUS_WMI_DEVID_WLAN_LED
880 * while setting the wlan status through WMI.
881 * This is also the behavior that windows app will do.
882 */
883 if ((dev_id == ASUS_WMI_DEVID_WLAN) &&
884 priv->asus->driver->wlan_ctrl_by_user)
885 dev_id = ASUS_WMI_DEVID_WLAN_LED;
886
887 return asus_wmi_set_devstate(dev_id, ctrl_param, NULL);
Corentin Charyba48fdb2010-11-29 08:14:07 +0100888}
889
Corentin Charye12e6d92011-02-26 10:20:31 +0100890static void asus_rfkill_query(struct rfkill *rfkill, void *data)
Corentin Charyba48fdb2010-11-29 08:14:07 +0100891{
Corentin Charya7ce3f02011-02-26 10:20:33 +0100892 struct asus_rfkill *priv = data;
Corentin Chary5c956382011-02-06 13:28:31 +0100893 int result;
Corentin Charyba48fdb2010-11-29 08:14:07 +0100894
Corentin Chary1d070f82011-02-26 10:20:36 +0100895 result = asus_wmi_get_devstate_simple(priv->asus, priv->dev_id);
Corentin Charyba48fdb2010-11-29 08:14:07 +0100896
Corentin Chary5c956382011-02-06 13:28:31 +0100897 if (result < 0)
Corentin Charye12e6d92011-02-26 10:20:31 +0100898 return;
Corentin Charyba48fdb2010-11-29 08:14:07 +0100899
Corentin Charya7ce3f02011-02-26 10:20:33 +0100900 rfkill_set_sw_state(priv->rfkill, !result);
Corentin Charyba48fdb2010-11-29 08:14:07 +0100901}
902
Corentin Charye12e6d92011-02-26 10:20:31 +0100903static int asus_rfkill_wlan_set(void *data, bool blocked)
Corentin Chary279f8f92011-02-06 13:28:29 +0100904{
Corentin Charya7ce3f02011-02-26 10:20:33 +0100905 struct asus_rfkill *priv = data;
906 struct asus_wmi *asus = priv->asus;
Corentin Chary279f8f92011-02-06 13:28:29 +0100907 int ret;
908
909 /*
910 * This handler is enabled only if hotplug is enabled.
Corentin Charye12e6d92011-02-26 10:20:31 +0100911 * In this case, the asus_wmi_set_devstate() will
Corentin Chary279f8f92011-02-06 13:28:29 +0100912 * trigger a wmi notification and we need to wait
913 * this call to finish before being able to call
914 * any wmi method
915 */
Corentin Charye12e6d92011-02-26 10:20:31 +0100916 mutex_lock(&asus->wmi_lock);
Corentin Charya7ce3f02011-02-26 10:20:33 +0100917 ret = asus_rfkill_set(data, blocked);
Corentin Charye12e6d92011-02-26 10:20:31 +0100918 mutex_unlock(&asus->wmi_lock);
Corentin Chary279f8f92011-02-06 13:28:29 +0100919 return ret;
920}
921
Corentin Charye12e6d92011-02-26 10:20:31 +0100922static const struct rfkill_ops asus_rfkill_wlan_ops = {
923 .set_block = asus_rfkill_wlan_set,
Corentin Charya7ce3f02011-02-26 10:20:33 +0100924 .query = asus_rfkill_query,
Corentin Chary279f8f92011-02-06 13:28:29 +0100925};
926
Corentin Charye12e6d92011-02-26 10:20:31 +0100927static const struct rfkill_ops asus_rfkill_ops = {
928 .set_block = asus_rfkill_set,
929 .query = asus_rfkill_query,
Corentin Charyba48fdb2010-11-29 08:14:07 +0100930};
931
Corentin Charye12e6d92011-02-26 10:20:31 +0100932static int asus_new_rfkill(struct asus_wmi *asus,
Corentin Charya7ce3f02011-02-26 10:20:33 +0100933 struct asus_rfkill *arfkill,
Corentin Charye12e6d92011-02-26 10:20:31 +0100934 const char *name, enum rfkill_type type, int dev_id)
Corentin Charyba48fdb2010-11-29 08:14:07 +0100935{
Corentin Chary1d070f82011-02-26 10:20:36 +0100936 int result = asus_wmi_get_devstate_simple(asus, dev_id);
Corentin Charya7ce3f02011-02-26 10:20:33 +0100937 struct rfkill **rfkill = &arfkill->rfkill;
Corentin Charyba48fdb2010-11-29 08:14:07 +0100938
Corentin Chary5c956382011-02-06 13:28:31 +0100939 if (result < 0)
940 return result;
Corentin Charyba48fdb2010-11-29 08:14:07 +0100941
Corentin Charya7ce3f02011-02-26 10:20:33 +0100942 arfkill->dev_id = dev_id;
943 arfkill->asus = asus;
944
AceLan Kaoc87992d2012-03-20 09:53:08 +0100945 if (dev_id == ASUS_WMI_DEVID_WLAN &&
946 asus->driver->quirks->hotplug_wireless)
Corentin Charye12e6d92011-02-26 10:20:31 +0100947 *rfkill = rfkill_alloc(name, &asus->platform_device->dev, type,
Corentin Charya7ce3f02011-02-26 10:20:33 +0100948 &asus_rfkill_wlan_ops, arfkill);
Corentin Chary279f8f92011-02-06 13:28:29 +0100949 else
Corentin Charye12e6d92011-02-26 10:20:31 +0100950 *rfkill = rfkill_alloc(name, &asus->platform_device->dev, type,
Corentin Charya7ce3f02011-02-26 10:20:33 +0100951 &asus_rfkill_ops, arfkill);
Corentin Charyba48fdb2010-11-29 08:14:07 +0100952
953 if (!*rfkill)
954 return -EINVAL;
955
AceLan Kaoe8f56c82013-05-30 10:31:50 +0800956 if ((dev_id == ASUS_WMI_DEVID_WLAN) &&
AceLan Kaof5156232014-07-09 16:18:18 +0800957 (asus->driver->quirks->wapf > 0))
AceLan Kao6cae06e2012-07-27 16:51:59 +0800958 rfkill_set_led_trigger_name(*rfkill, "asus-wlan");
959
Corentin Chary5c956382011-02-06 13:28:31 +0100960 rfkill_init_sw_state(*rfkill, !result);
Corentin Charyba48fdb2010-11-29 08:14:07 +0100961 result = rfkill_register(*rfkill);
962 if (result) {
963 rfkill_destroy(*rfkill);
964 *rfkill = NULL;
965 return result;
966 }
967 return 0;
968}
969
Corentin Charye12e6d92011-02-26 10:20:31 +0100970static void asus_wmi_rfkill_exit(struct asus_wmi *asus)
Corentin Charyba48fdb2010-11-29 08:14:07 +0100971{
João Paulo Rechi Vitacf48bf92018-05-22 14:30:15 -0700972 if (asus->driver->wlan_ctrl_by_user && ashs_present())
973 return;
974
Corentin Charye12e6d92011-02-26 10:20:31 +0100975 asus_unregister_rfkill_notifier(asus, "\\_SB.PCI0.P0P5");
976 asus_unregister_rfkill_notifier(asus, "\\_SB.PCI0.P0P6");
977 asus_unregister_rfkill_notifier(asus, "\\_SB.PCI0.P0P7");
Corentin Charya7ce3f02011-02-26 10:20:33 +0100978 if (asus->wlan.rfkill) {
979 rfkill_unregister(asus->wlan.rfkill);
980 rfkill_destroy(asus->wlan.rfkill);
981 asus->wlan.rfkill = NULL;
Corentin Charyba48fdb2010-11-29 08:14:07 +0100982 }
Corentin Charyafa7c882011-02-06 13:28:28 +0100983 /*
984 * Refresh pci hotplug in case the rfkill state was changed after
Corentin Charye12e6d92011-02-26 10:20:31 +0100985 * asus_unregister_rfkill_notifier()
Corentin Charyafa7c882011-02-06 13:28:28 +0100986 */
Corentin Charye12e6d92011-02-26 10:20:31 +0100987 asus_rfkill_hotplug(asus);
Lukas Wunner125450f2018-09-08 09:59:01 +0200988 if (asus->hotplug_slot.ops)
989 pci_hp_deregister(&asus->hotplug_slot);
Corentin Charye12e6d92011-02-26 10:20:31 +0100990 if (asus->hotplug_workqueue)
991 destroy_workqueue(asus->hotplug_workqueue);
Corentin Charyafa7c882011-02-06 13:28:28 +0100992
Corentin Charya7ce3f02011-02-26 10:20:33 +0100993 if (asus->bluetooth.rfkill) {
994 rfkill_unregister(asus->bluetooth.rfkill);
995 rfkill_destroy(asus->bluetooth.rfkill);
996 asus->bluetooth.rfkill = NULL;
Corentin Charyba48fdb2010-11-29 08:14:07 +0100997 }
Corentin Charya7ce3f02011-02-26 10:20:33 +0100998 if (asus->wimax.rfkill) {
999 rfkill_unregister(asus->wimax.rfkill);
1000 rfkill_destroy(asus->wimax.rfkill);
1001 asus->wimax.rfkill = NULL;
Corentin Chary2e9e1592011-02-06 13:28:37 +01001002 }
Corentin Charya7ce3f02011-02-26 10:20:33 +01001003 if (asus->wwan3g.rfkill) {
1004 rfkill_unregister(asus->wwan3g.rfkill);
1005 rfkill_destroy(asus->wwan3g.rfkill);
1006 asus->wwan3g.rfkill = NULL;
Corentin Charyba48fdb2010-11-29 08:14:07 +01001007 }
Corentin Chary43be8bd2011-07-01 11:34:40 +02001008 if (asus->gps.rfkill) {
1009 rfkill_unregister(asus->gps.rfkill);
1010 rfkill_destroy(asus->gps.rfkill);
1011 asus->gps.rfkill = NULL;
1012 }
Corentin Charya912d322011-07-01 11:34:41 +02001013 if (asus->uwb.rfkill) {
1014 rfkill_unregister(asus->uwb.rfkill);
1015 rfkill_destroy(asus->uwb.rfkill);
1016 asus->uwb.rfkill = NULL;
1017 }
Corentin Charyba48fdb2010-11-29 08:14:07 +01001018}
1019
Corentin Charye12e6d92011-02-26 10:20:31 +01001020static int asus_wmi_rfkill_init(struct asus_wmi *asus)
Corentin Charyba48fdb2010-11-29 08:14:07 +01001021{
1022 int result = 0;
1023
Corentin Charye12e6d92011-02-26 10:20:31 +01001024 mutex_init(&asus->hotplug_lock);
1025 mutex_init(&asus->wmi_lock);
Corentin Charyafa7c882011-02-06 13:28:28 +01001026
Corentin Charya7ce3f02011-02-26 10:20:33 +01001027 result = asus_new_rfkill(asus, &asus->wlan, "asus-wlan",
1028 RFKILL_TYPE_WLAN, ASUS_WMI_DEVID_WLAN);
Corentin Charyba48fdb2010-11-29 08:14:07 +01001029
1030 if (result && result != -ENODEV)
1031 goto exit;
1032
Corentin Charya7ce3f02011-02-26 10:20:33 +01001033 result = asus_new_rfkill(asus, &asus->bluetooth,
Corentin Charye12e6d92011-02-26 10:20:31 +01001034 "asus-bluetooth", RFKILL_TYPE_BLUETOOTH,
1035 ASUS_WMI_DEVID_BLUETOOTH);
Corentin Charyba48fdb2010-11-29 08:14:07 +01001036
1037 if (result && result != -ENODEV)
1038 goto exit;
1039
Corentin Charya7ce3f02011-02-26 10:20:33 +01001040 result = asus_new_rfkill(asus, &asus->wimax, "asus-wimax",
1041 RFKILL_TYPE_WIMAX, ASUS_WMI_DEVID_WIMAX);
Corentin Chary2e9e1592011-02-06 13:28:37 +01001042
1043 if (result && result != -ENODEV)
1044 goto exit;
1045
Corentin Charya7ce3f02011-02-26 10:20:33 +01001046 result = asus_new_rfkill(asus, &asus->wwan3g, "asus-wwan3g",
1047 RFKILL_TYPE_WWAN, ASUS_WMI_DEVID_WWAN3G);
Corentin Charyba48fdb2010-11-29 08:14:07 +01001048
1049 if (result && result != -ENODEV)
1050 goto exit;
1051
Corentin Chary43be8bd2011-07-01 11:34:40 +02001052 result = asus_new_rfkill(asus, &asus->gps, "asus-gps",
1053 RFKILL_TYPE_GPS, ASUS_WMI_DEVID_GPS);
1054
1055 if (result && result != -ENODEV)
1056 goto exit;
1057
Corentin Charya912d322011-07-01 11:34:41 +02001058 result = asus_new_rfkill(asus, &asus->uwb, "asus-uwb",
1059 RFKILL_TYPE_UWB, ASUS_WMI_DEVID_UWB);
1060
1061 if (result && result != -ENODEV)
1062 goto exit;
1063
AceLan Kaoc87992d2012-03-20 09:53:08 +01001064 if (!asus->driver->quirks->hotplug_wireless)
Corentin Charyc14d4b82011-02-06 13:28:40 +01001065 goto exit;
1066
Corentin Charye12e6d92011-02-26 10:20:31 +01001067 result = asus_setup_pci_hotplug(asus);
Corentin Charyafa7c882011-02-06 13:28:28 +01001068 /*
1069 * If we get -EBUSY then something else is handling the PCI hotplug -
1070 * don't fail in this case
1071 */
1072 if (result == -EBUSY)
1073 result = 0;
1074
Corentin Charye12e6d92011-02-26 10:20:31 +01001075 asus_register_rfkill_notifier(asus, "\\_SB.PCI0.P0P5");
1076 asus_register_rfkill_notifier(asus, "\\_SB.PCI0.P0P6");
1077 asus_register_rfkill_notifier(asus, "\\_SB.PCI0.P0P7");
Corentin Charyafa7c882011-02-06 13:28:28 +01001078 /*
1079 * Refresh pci hotplug in case the rfkill state was changed during
1080 * setup.
1081 */
Corentin Charye12e6d92011-02-26 10:20:31 +01001082 asus_rfkill_hotplug(asus);
Corentin Charyafa7c882011-02-06 13:28:28 +01001083
Corentin Charyba48fdb2010-11-29 08:14:07 +01001084exit:
1085 if (result && result != -ENODEV)
Corentin Charye12e6d92011-02-26 10:20:31 +01001086 asus_wmi_rfkill_exit(asus);
Corentin Charyba48fdb2010-11-29 08:14:07 +01001087
1088 if (result == -ENODEV)
1089 result = 0;
1090
1091 return result;
1092}
1093
Yurii Pavlovskyi54a31212019-05-14 21:04:48 +02001094/* Quirks *********************************************************************/
1095
Kai-Chuan Hsieh8023eff2016-09-01 23:55:55 +08001096static void asus_wmi_set_xusb2pr(struct asus_wmi *asus)
1097{
1098 struct pci_dev *xhci_pdev;
1099 u32 orig_ports_available;
1100 u32 ports_available = asus->driver->quirks->xusb2pr;
1101
1102 xhci_pdev = pci_get_device(PCI_VENDOR_ID_INTEL,
1103 PCI_DEVICE_ID_INTEL_LYNXPOINT_LP_XHCI,
1104 NULL);
1105
1106 if (!xhci_pdev)
1107 return;
1108
1109 pci_read_config_dword(xhci_pdev, USB_INTEL_XUSB2PR,
1110 &orig_ports_available);
1111
1112 pci_write_config_dword(xhci_pdev, USB_INTEL_XUSB2PR,
1113 cpu_to_le32(ports_available));
1114
1115 pr_info("set USB_INTEL_XUSB2PR old: 0x%04x, new: 0x%04x\n",
1116 orig_ports_available, ports_available);
1117}
1118
Corentin Charyba48fdb2010-11-29 08:14:07 +01001119/*
Oleksij Rempele9b61512017-04-28 16:19:49 +02001120 * Some devices dont support or have borcken get_als method
1121 * but still support set method.
1122 */
1123static void asus_wmi_set_als(void)
1124{
1125 asus_wmi_set_devstate(ASUS_WMI_DEVID_ALS_ENABLE, 1, NULL);
1126}
1127
Yurii Pavlovskyi54a31212019-05-14 21:04:48 +02001128/* Hwmon device ***************************************************************/
1129
Daniel Drake2889ffc2019-07-29 16:27:37 +08001130static int asus_agfn_fan_speed_read(struct asus_wmi *asus, int fan,
Kast Bernd53e755c2015-05-13 16:24:16 +02001131 int *speed)
1132{
Daniel Drake2889ffc2019-07-29 16:27:37 +08001133 struct agfn_fan_args args = {
Kast Bernd53e755c2015-05-13 16:24:16 +02001134 .agfn.len = sizeof(args),
1135 .agfn.mfun = ASUS_FAN_MFUN,
1136 .agfn.sfun = ASUS_FAN_SFUN_READ,
1137 .fan = fan,
1138 .speed = 0,
1139 };
1140 struct acpi_buffer input = { (acpi_size) sizeof(args), &args };
1141 int status;
1142
1143 if (fan != 1)
1144 return -EINVAL;
1145
1146 status = asus_wmi_evaluate_method_agfn(input);
1147
1148 if (status || args.agfn.err)
1149 return -ENXIO;
1150
1151 if (speed)
1152 *speed = args.speed;
1153
1154 return 0;
1155}
1156
Daniel Drake2889ffc2019-07-29 16:27:37 +08001157static int asus_agfn_fan_speed_write(struct asus_wmi *asus, int fan,
Kast Bernd53e755c2015-05-13 16:24:16 +02001158 int *speed)
1159{
Daniel Drake2889ffc2019-07-29 16:27:37 +08001160 struct agfn_fan_args args = {
Kast Bernd53e755c2015-05-13 16:24:16 +02001161 .agfn.len = sizeof(args),
1162 .agfn.mfun = ASUS_FAN_MFUN,
1163 .agfn.sfun = ASUS_FAN_SFUN_WRITE,
1164 .fan = fan,
1165 .speed = speed ? *speed : 0,
1166 };
1167 struct acpi_buffer input = { (acpi_size) sizeof(args), &args };
1168 int status;
1169
1170 /* 1: for setting 1st fan's speed 0: setting auto mode */
1171 if (fan != 1 && fan != 0)
1172 return -EINVAL;
1173
1174 status = asus_wmi_evaluate_method_agfn(input);
1175
1176 if (status || args.agfn.err)
1177 return -ENXIO;
1178
1179 if (speed && fan == 1)
Daniel Drake2889ffc2019-07-29 16:27:37 +08001180 asus->agfn_pwm = *speed;
Kast Bernd53e755c2015-05-13 16:24:16 +02001181
1182 return 0;
1183}
1184
1185/*
1186 * Check if we can read the speed of one fan. If true we assume we can also
1187 * control it.
1188 */
Daniel Drake2889ffc2019-07-29 16:27:37 +08001189static bool asus_wmi_has_agfn_fan(struct asus_wmi *asus)
Kast Bernd53e755c2015-05-13 16:24:16 +02001190{
1191 int status;
Daniel Drake2889ffc2019-07-29 16:27:37 +08001192 int speed;
1193 u32 value;
Kast Bernd53e755c2015-05-13 16:24:16 +02001194
Daniel Drake2889ffc2019-07-29 16:27:37 +08001195 status = asus_agfn_fan_speed_read(asus, 1, &speed);
1196 if (status != 0)
1197 return false;
Kast Bernd53e755c2015-05-13 16:24:16 +02001198
Daniel Drake2889ffc2019-07-29 16:27:37 +08001199 status = asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_FAN_CTRL, &value);
1200 if (status != 0)
1201 return false;
Kast Bernd53e755c2015-05-13 16:24:16 +02001202
Daniel Drake2889ffc2019-07-29 16:27:37 +08001203 /*
1204 * We need to find a better way, probably using sfun,
1205 * bits or spec ...
1206 * Currently we disable it if:
1207 * - ASUS_WMI_UNSUPPORTED_METHOD is returned
1208 * - reverved bits are non-zero
1209 * - sfun and presence bit are not set
1210 */
1211 return !(value == ASUS_WMI_UNSUPPORTED_METHOD || value & 0xFFF80000
1212 || (!asus->sfun && !(value & ASUS_WMI_DSTS_PRESENCE_BIT)));
Kast Bernd53e755c2015-05-13 16:24:16 +02001213}
1214
Daniel Drake2889ffc2019-07-29 16:27:37 +08001215static int asus_fan_set_auto(struct asus_wmi *asus)
Kast Bernd53e755c2015-05-13 16:24:16 +02001216{
1217 int status;
Daniel Drakee3168b82019-07-29 16:27:39 +08001218 u32 retval;
Kast Bernd53e755c2015-05-13 16:24:16 +02001219
Daniel Drakee3168b82019-07-29 16:27:39 +08001220 switch (asus->fan_type) {
1221 case FAN_TYPE_SPEC83:
1222 status = asus_wmi_set_devstate(ASUS_WMI_DEVID_CPU_FAN_CTRL,
1223 0, &retval);
1224 if (status)
1225 return status;
1226
1227 if (retval != 1)
1228 return -EIO;
1229 break;
1230
1231 case FAN_TYPE_AGFN:
1232 status = asus_agfn_fan_speed_write(asus, 0, NULL);
1233 if (status)
1234 return -ENXIO;
1235 break;
1236
1237 default:
Kast Bernd53e755c2015-05-13 16:24:16 +02001238 return -ENXIO;
Daniel Drakee3168b82019-07-29 16:27:39 +08001239 }
1240
Kast Bernd53e755c2015-05-13 16:24:16 +02001241
Kast Bernd53e755c2015-05-13 16:24:16 +02001242 return 0;
1243}
1244
Kast Bernd53e755c2015-05-13 16:24:16 +02001245static ssize_t pwm1_show(struct device *dev,
Corentin Chary49979d02011-07-01 11:34:26 +02001246 struct device_attribute *attr,
1247 char *buf)
Corentin Charye07babd2011-02-26 10:20:42 +01001248{
1249 struct asus_wmi *asus = dev_get_drvdata(dev);
Daniel Drake2889ffc2019-07-29 16:27:37 +08001250 int err;
Kast Bernd53e755c2015-05-13 16:24:16 +02001251 int value;
Corentin Charye07babd2011-02-26 10:20:42 +01001252
Daniel Drake2889ffc2019-07-29 16:27:37 +08001253 /* If we already set a value then just return it */
1254 if (asus->agfn_pwm >= 0)
1255 return sprintf(buf, "%d\n", asus->agfn_pwm);
1256
1257 /*
1258 * If we haven't set already set a value through the AGFN interface,
1259 * we read a current value through the (now-deprecated) FAN_CTRL device.
1260 */
1261 err = asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_FAN_CTRL, &value);
1262 if (err < 0)
1263 return err;
1264
1265 value &= 0xFF;
1266
1267 if (value == 1) /* Low Speed */
1268 value = 85;
1269 else if (value == 2)
1270 value = 170;
1271 else if (value == 3)
1272 value = 255;
1273 else if (value) {
1274 pr_err("Unknown fan speed %#x\n", value);
1275 value = -1;
1276 }
Corentin Charye07babd2011-02-26 10:20:42 +01001277
1278 return sprintf(buf, "%d\n", value);
1279}
1280
Kast Bernd53e755c2015-05-13 16:24:16 +02001281static ssize_t pwm1_store(struct device *dev,
1282 struct device_attribute *attr,
1283 const char *buf, size_t count) {
1284 struct asus_wmi *asus = dev_get_drvdata(dev);
1285 int value;
1286 int state;
1287 int ret;
1288
1289 ret = kstrtouint(buf, 10, &value);
1290
1291 if (ret)
1292 return ret;
1293
1294 value = clamp(value, 0, 255);
1295
Daniel Drake2889ffc2019-07-29 16:27:37 +08001296 state = asus_agfn_fan_speed_write(asus, 1, &value);
Kast Bernd53e755c2015-05-13 16:24:16 +02001297 if (state)
1298 pr_warn("Setting fan speed failed: %d\n", state);
1299 else
Daniel Drake2889ffc2019-07-29 16:27:37 +08001300 asus->fan_pwm_mode = ASUS_FAN_CTRL_MANUAL;
Kast Bernd53e755c2015-05-13 16:24:16 +02001301
1302 return count;
1303}
1304
1305static ssize_t fan1_input_show(struct device *dev,
1306 struct device_attribute *attr,
1307 char *buf)
1308{
Daniel Drake2889ffc2019-07-29 16:27:37 +08001309 struct asus_wmi *asus = dev_get_drvdata(dev);
1310 int value;
1311 int ret;
1312
Daniel Drakee3168b82019-07-29 16:27:39 +08001313 switch (asus->fan_type) {
1314 case FAN_TYPE_SPEC83:
1315 ret = asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_CPU_FAN_CTRL,
1316 &value);
1317 if (ret < 0)
1318 return ret;
Daniel Drake2889ffc2019-07-29 16:27:37 +08001319
Daniel Drakee3168b82019-07-29 16:27:39 +08001320 value &= 0xffff;
1321 break;
1322
1323 case FAN_TYPE_AGFN:
1324 /* no speed readable on manual mode */
1325 if (asus->fan_pwm_mode == ASUS_FAN_CTRL_MANUAL)
1326 return -ENXIO;
1327
1328 ret = asus_agfn_fan_speed_read(asus, 1, &value);
1329 if (ret) {
1330 pr_warn("reading fan speed failed: %d\n", ret);
1331 return -ENXIO;
1332 }
1333 break;
1334
1335 default:
Daniel Drake2889ffc2019-07-29 16:27:37 +08001336 return -ENXIO;
1337 }
Kast Bernd53e755c2015-05-13 16:24:16 +02001338
1339 return sprintf(buf, "%d\n", value < 0 ? -1 : value*100);
Kast Bernd53e755c2015-05-13 16:24:16 +02001340}
1341
1342static ssize_t pwm1_enable_show(struct device *dev,
1343 struct device_attribute *attr,
1344 char *buf)
1345{
1346 struct asus_wmi *asus = dev_get_drvdata(dev);
1347
Daniel Drakee3168b82019-07-29 16:27:39 +08001348 /*
1349 * Just read back the cached pwm mode.
1350 *
1351 * For the CPU_FAN device, the spec indicates that we should be
1352 * able to read the device status and consult bit 19 to see if we
1353 * are in Full On or Automatic mode. However, this does not work
1354 * in practice on X532FL at least (the bit is always 0) and there's
1355 * also nothing in the DSDT to indicate that this behaviour exists.
1356 */
Daniel Drake2889ffc2019-07-29 16:27:37 +08001357 return sprintf(buf, "%d\n", asus->fan_pwm_mode);
Kast Bernd53e755c2015-05-13 16:24:16 +02001358}
1359
1360static ssize_t pwm1_enable_store(struct device *dev,
1361 struct device_attribute *attr,
1362 const char *buf, size_t count)
1363{
1364 struct asus_wmi *asus = dev_get_drvdata(dev);
1365 int status = 0;
1366 int state;
Daniel Drakee3168b82019-07-29 16:27:39 +08001367 int value;
Kast Bernd53e755c2015-05-13 16:24:16 +02001368 int ret;
Daniel Drakee3168b82019-07-29 16:27:39 +08001369 u32 retval;
Kast Bernd53e755c2015-05-13 16:24:16 +02001370
1371 ret = kstrtouint(buf, 10, &state);
1372
1373 if (ret)
1374 return ret;
1375
Daniel Drakee3168b82019-07-29 16:27:39 +08001376 if (asus->fan_type == FAN_TYPE_SPEC83) {
1377 switch (state) { /* standard documented hwmon values */
1378 case ASUS_FAN_CTRL_FULLSPEED:
1379 value = 1;
1380 break;
1381 case ASUS_FAN_CTRL_AUTO:
1382 value = 0;
1383 break;
1384 default:
1385 return -EINVAL;
1386 }
Kast Bernd53e755c2015-05-13 16:24:16 +02001387
Daniel Drakee3168b82019-07-29 16:27:39 +08001388 ret = asus_wmi_set_devstate(ASUS_WMI_DEVID_CPU_FAN_CTRL,
1389 value, &retval);
1390 if (ret)
1391 return ret;
Kast Bernd53e755c2015-05-13 16:24:16 +02001392
Daniel Drakee3168b82019-07-29 16:27:39 +08001393 if (retval != 1)
1394 return -EIO;
1395 } else if (asus->fan_type == FAN_TYPE_AGFN) {
1396 switch (state) {
1397 case ASUS_FAN_CTRL_MANUAL:
1398 break;
1399
1400 case ASUS_FAN_CTRL_AUTO:
1401 status = asus_fan_set_auto(asus);
1402 if (status)
1403 return status;
1404 break;
1405
1406 default:
1407 return -EINVAL;
1408 }
Daniel Drake2889ffc2019-07-29 16:27:37 +08001409 }
1410
1411 asus->fan_pwm_mode = state;
Kast Bernd53e755c2015-05-13 16:24:16 +02001412 return count;
1413}
1414
1415static ssize_t fan1_label_show(struct device *dev,
1416 struct device_attribute *attr,
1417 char *buf)
1418{
1419 return sprintf(buf, "%s\n", ASUS_FAN_DESC);
1420}
1421
Corentin Chary6118b8a2011-07-01 11:34:36 +02001422static ssize_t asus_hwmon_temp1(struct device *dev,
1423 struct device_attribute *attr,
1424 char *buf)
1425{
1426 struct asus_wmi *asus = dev_get_drvdata(dev);
1427 u32 value;
1428 int err;
1429
1430 err = asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_THERMAL_CTRL, &value);
1431
1432 if (err < 0)
1433 return err;
1434
Rasmus Villemoese866a2e2015-10-01 23:45:31 +02001435 value = DECI_KELVIN_TO_CELSIUS((value & 0xFFFF)) * 1000;
Corentin Chary6118b8a2011-07-01 11:34:36 +02001436
1437 return sprintf(buf, "%d\n", value);
1438}
1439
Kast Bernd53e755c2015-05-13 16:24:16 +02001440/* Fan1 */
1441static DEVICE_ATTR_RW(pwm1);
1442static DEVICE_ATTR_RW(pwm1_enable);
1443static DEVICE_ATTR_RO(fan1_input);
1444static DEVICE_ATTR_RO(fan1_label);
1445
1446/* Temperature */
Guenter Roeck50a639f2013-11-23 11:03:17 -08001447static DEVICE_ATTR(temp1_input, S_IRUGO, asus_hwmon_temp1, NULL);
Corentin Charye07babd2011-02-26 10:20:42 +01001448
1449static struct attribute *hwmon_attributes[] = {
Guenter Roeck50a639f2013-11-23 11:03:17 -08001450 &dev_attr_pwm1.attr,
Kast Bernd53e755c2015-05-13 16:24:16 +02001451 &dev_attr_pwm1_enable.attr,
1452 &dev_attr_fan1_input.attr,
1453 &dev_attr_fan1_label.attr,
1454
Guenter Roeck50a639f2013-11-23 11:03:17 -08001455 &dev_attr_temp1_input.attr,
Corentin Charye07babd2011-02-26 10:20:42 +01001456 NULL
1457};
1458
Al Viro587a1f12011-07-23 23:11:19 -04001459static umode_t asus_hwmon_sysfs_is_visible(struct kobject *kobj,
Corentin Charye02431d2011-07-01 11:34:37 +02001460 struct attribute *attr, int idx)
Corentin Charye07babd2011-02-26 10:20:42 +01001461{
1462 struct device *dev = container_of(kobj, struct device, kobj);
Fuqian Huang8e8fe442019-07-01 11:24:26 +08001463 struct asus_wmi *asus = dev_get_drvdata(dev->parent);
Corentin Charye07babd2011-02-26 10:20:42 +01001464 u32 value = ASUS_WMI_UNSUPPORTED_METHOD;
Kast Bernd53e755c2015-05-13 16:24:16 +02001465
Daniel Drakee3168b82019-07-29 16:27:39 +08001466 if (attr == &dev_attr_pwm1.attr) {
1467 if (asus->fan_type != FAN_TYPE_AGFN)
1468 return 0;
1469 } else if (attr == &dev_attr_fan1_input.attr
Kast Bernd53e755c2015-05-13 16:24:16 +02001470 || attr == &dev_attr_fan1_label.attr
Kast Bernd53e755c2015-05-13 16:24:16 +02001471 || attr == &dev_attr_pwm1_enable.attr) {
Daniel Drake2889ffc2019-07-29 16:27:37 +08001472 if (asus->fan_type == FAN_TYPE_NONE)
1473 return 0;
1474 } else if (attr == &dev_attr_temp1_input.attr) {
1475 int err = asus_wmi_get_devstate(asus,
1476 ASUS_WMI_DEVID_THERMAL_CTRL,
1477 &value);
Kast Bernd53e755c2015-05-13 16:24:16 +02001478
Daniel Drake2889ffc2019-07-29 16:27:37 +08001479 if (err < 0)
Al Viroe772aed2011-07-23 20:59:40 -04001480 return 0; /* can't return negative here */
Corentin Charye07babd2011-02-26 10:20:42 +01001481
Yurii Pavlovskyi4fd19822019-05-14 21:05:46 +02001482 /*
1483 * If the temperature value in deci-Kelvin is near the absolute
1484 * zero temperature, something is clearly wrong
1485 */
1486 if (value == 0 || value == 1)
Daniel Drake2889ffc2019-07-29 16:27:37 +08001487 return 0;
Corentin Charye07babd2011-02-26 10:20:42 +01001488 }
1489
Daniel Drake2889ffc2019-07-29 16:27:37 +08001490 return attr->mode;
Corentin Charye07babd2011-02-26 10:20:42 +01001491}
1492
Arvind Yadave90d9ba2017-07-11 16:18:19 +05301493static const struct attribute_group hwmon_attribute_group = {
Corentin Charye07babd2011-02-26 10:20:42 +01001494 .is_visible = asus_hwmon_sysfs_is_visible,
1495 .attrs = hwmon_attributes
1496};
Guenter Roeck50a639f2013-11-23 11:03:17 -08001497__ATTRIBUTE_GROUPS(hwmon_attribute);
Corentin Charye07babd2011-02-26 10:20:42 +01001498
1499static int asus_wmi_hwmon_init(struct asus_wmi *asus)
1500{
Yurii Pavlovskyicd10ee02019-05-14 20:50:30 +02001501 struct device *dev = &asus->platform_device->dev;
Corentin Charye07babd2011-02-26 10:20:42 +01001502 struct device *hwmon;
Corentin Charye07babd2011-02-26 10:20:42 +01001503
Yurii Pavlovskyicd10ee02019-05-14 20:50:30 +02001504 hwmon = devm_hwmon_device_register_with_groups(dev, "asus", asus,
1505 hwmon_attribute_groups);
1506
Corentin Charye07babd2011-02-26 10:20:42 +01001507 if (IS_ERR(hwmon)) {
1508 pr_err("Could not register asus hwmon device\n");
1509 return PTR_ERR(hwmon);
1510 }
Guenter Roeck50a639f2013-11-23 11:03:17 -08001511 return 0;
Corentin Charye07babd2011-02-26 10:20:42 +01001512}
1513
Yurii Pavlovskyi54a31212019-05-14 21:04:48 +02001514static int asus_wmi_fan_init(struct asus_wmi *asus)
1515{
Daniel Drake2889ffc2019-07-29 16:27:37 +08001516 asus->fan_type = FAN_TYPE_NONE;
1517 asus->agfn_pwm = -1;
Yurii Pavlovskyi54a31212019-05-14 21:04:48 +02001518
Daniel Drakee3168b82019-07-29 16:27:39 +08001519 if (asus_wmi_dev_is_present(asus, ASUS_WMI_DEVID_CPU_FAN_CTRL))
1520 asus->fan_type = FAN_TYPE_SPEC83;
1521 else if (asus_wmi_has_agfn_fan(asus))
Daniel Drake2889ffc2019-07-29 16:27:37 +08001522 asus->fan_type = FAN_TYPE_AGFN;
Yurii Pavlovskyi54a31212019-05-14 21:04:48 +02001523
Daniel Drakee3168b82019-07-29 16:27:39 +08001524 if (asus->fan_type == FAN_TYPE_NONE)
1525 return -ENODEV;
1526
1527 asus_fan_set_auto(asus);
1528 asus->fan_pwm_mode = ASUS_FAN_CTRL_AUTO;
1529 return 0;
Yurii Pavlovskyi54a31212019-05-14 21:04:48 +02001530}
1531
Yurii Pavlovskyib096f622019-05-14 21:07:05 +02001532/* Fan mode *******************************************************************/
1533
Daniel Drake9af93db2019-07-17 13:10:58 +08001534static int fan_boost_mode_check_present(struct asus_wmi *asus)
Yurii Pavlovskyib096f622019-05-14 21:07:05 +02001535{
1536 u32 result;
1537 int err;
1538
Daniel Drake9af93db2019-07-17 13:10:58 +08001539 asus->fan_boost_mode_available = false;
Yurii Pavlovskyib096f622019-05-14 21:07:05 +02001540
Daniel Drake9af93db2019-07-17 13:10:58 +08001541 err = asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_FAN_BOOST_MODE,
1542 &result);
Yurii Pavlovskyib096f622019-05-14 21:07:05 +02001543 if (err) {
1544 if (err == -ENODEV)
1545 return 0;
1546 else
1547 return err;
1548 }
1549
1550 if ((result & ASUS_WMI_DSTS_PRESENCE_BIT) &&
Daniel Drake9af93db2019-07-17 13:10:58 +08001551 (result & ASUS_FAN_BOOST_MODES_MASK)) {
1552 asus->fan_boost_mode_available = true;
1553 asus->fan_boost_mode_mask = result & ASUS_FAN_BOOST_MODES_MASK;
Yurii Pavlovskyib096f622019-05-14 21:07:05 +02001554 }
1555
1556 return 0;
1557}
1558
Daniel Drake9af93db2019-07-17 13:10:58 +08001559static int fan_boost_mode_write(struct asus_wmi *asus)
Yurii Pavlovskyib096f622019-05-14 21:07:05 +02001560{
1561 int err;
1562 u8 value;
1563 u32 retval;
1564
Daniel Drake9af93db2019-07-17 13:10:58 +08001565 value = asus->fan_boost_mode;
Yurii Pavlovskyib096f622019-05-14 21:07:05 +02001566
Daniel Drake9af93db2019-07-17 13:10:58 +08001567 pr_info("Set fan boost mode: %u\n", value);
1568 err = asus_wmi_set_devstate(ASUS_WMI_DEVID_FAN_BOOST_MODE, value,
1569 &retval);
Yurii Pavlovskyib096f622019-05-14 21:07:05 +02001570
1571 if (err) {
Daniel Drake9af93db2019-07-17 13:10:58 +08001572 pr_warn("Failed to set fan boost mode: %d\n", err);
Yurii Pavlovskyib096f622019-05-14 21:07:05 +02001573 return err;
1574 }
1575
1576 if (retval != 1) {
Daniel Drake9af93db2019-07-17 13:10:58 +08001577 pr_warn("Failed to set fan boost mode (retval): 0x%x\n",
1578 retval);
Yurii Pavlovskyib096f622019-05-14 21:07:05 +02001579 return -EIO;
1580 }
1581
1582 return 0;
1583}
1584
Daniel Drake9af93db2019-07-17 13:10:58 +08001585static int fan_boost_mode_switch_next(struct asus_wmi *asus)
Yurii Pavlovskyib096f622019-05-14 21:07:05 +02001586{
Daniel Drake9af93db2019-07-17 13:10:58 +08001587 u8 mask = asus->fan_boost_mode_mask;
1588
1589 if (asus->fan_boost_mode == ASUS_FAN_BOOST_MODE_NORMAL) {
1590 if (mask & ASUS_FAN_BOOST_MODE_OVERBOOST_MASK)
1591 asus->fan_boost_mode = ASUS_FAN_BOOST_MODE_OVERBOOST;
1592 else if (mask & ASUS_FAN_BOOST_MODE_SILENT_MASK)
1593 asus->fan_boost_mode = ASUS_FAN_BOOST_MODE_SILENT;
1594 } else if (asus->fan_boost_mode == ASUS_FAN_BOOST_MODE_OVERBOOST) {
1595 if (mask & ASUS_FAN_BOOST_MODE_SILENT_MASK)
1596 asus->fan_boost_mode = ASUS_FAN_BOOST_MODE_SILENT;
Yurii Pavlovskyib096f622019-05-14 21:07:05 +02001597 else
Daniel Drake9af93db2019-07-17 13:10:58 +08001598 asus->fan_boost_mode = ASUS_FAN_BOOST_MODE_NORMAL;
Yurii Pavlovskyib096f622019-05-14 21:07:05 +02001599 } else {
Daniel Drake9af93db2019-07-17 13:10:58 +08001600 asus->fan_boost_mode = ASUS_FAN_BOOST_MODE_NORMAL;
Yurii Pavlovskyib096f622019-05-14 21:07:05 +02001601 }
1602
Daniel Drake9af93db2019-07-17 13:10:58 +08001603 return fan_boost_mode_write(asus);
Yurii Pavlovskyib096f622019-05-14 21:07:05 +02001604}
1605
Daniel Drake9af93db2019-07-17 13:10:58 +08001606static ssize_t fan_boost_mode_show(struct device *dev,
1607 struct device_attribute *attr, char *buf)
Yurii Pavlovskyib096f622019-05-14 21:07:05 +02001608{
1609 struct asus_wmi *asus = dev_get_drvdata(dev);
1610
Daniel Drake9af93db2019-07-17 13:10:58 +08001611 return scnprintf(buf, PAGE_SIZE, "%d\n", asus->fan_boost_mode);
Yurii Pavlovskyib096f622019-05-14 21:07:05 +02001612}
1613
Daniel Drake9af93db2019-07-17 13:10:58 +08001614static ssize_t fan_boost_mode_store(struct device *dev,
1615 struct device_attribute *attr,
1616 const char *buf, size_t count)
Yurii Pavlovskyib096f622019-05-14 21:07:05 +02001617{
1618 int result;
1619 u8 new_mode;
Yurii Pavlovskyib096f622019-05-14 21:07:05 +02001620 struct asus_wmi *asus = dev_get_drvdata(dev);
Daniel Drake9af93db2019-07-17 13:10:58 +08001621 u8 mask = asus->fan_boost_mode_mask;
Yurii Pavlovskyib096f622019-05-14 21:07:05 +02001622
1623 result = kstrtou8(buf, 10, &new_mode);
1624 if (result < 0) {
1625 pr_warn("Trying to store invalid value\n");
1626 return result;
1627 }
1628
Daniel Drake9af93db2019-07-17 13:10:58 +08001629 if (new_mode == ASUS_FAN_BOOST_MODE_OVERBOOST) {
1630 if (!(mask & ASUS_FAN_BOOST_MODE_OVERBOOST_MASK))
Yurii Pavlovskyib096f622019-05-14 21:07:05 +02001631 return -EINVAL;
Daniel Drake9af93db2019-07-17 13:10:58 +08001632 } else if (new_mode == ASUS_FAN_BOOST_MODE_SILENT) {
1633 if (!(mask & ASUS_FAN_BOOST_MODE_SILENT_MASK))
Yurii Pavlovskyib096f622019-05-14 21:07:05 +02001634 return -EINVAL;
Daniel Drake9af93db2019-07-17 13:10:58 +08001635 } else if (new_mode != ASUS_FAN_BOOST_MODE_NORMAL) {
Yurii Pavlovskyib096f622019-05-14 21:07:05 +02001636 return -EINVAL;
1637 }
1638
Daniel Drake9af93db2019-07-17 13:10:58 +08001639 asus->fan_boost_mode = new_mode;
1640 fan_boost_mode_write(asus);
Yurii Pavlovskyib096f622019-05-14 21:07:05 +02001641
1642 return result;
1643}
1644
Daniel Drake9af93db2019-07-17 13:10:58 +08001645// Fan boost mode: 0 - normal, 1 - overboost, 2 - silent
1646static DEVICE_ATTR_RW(fan_boost_mode);
Yurii Pavlovskyib096f622019-05-14 21:07:05 +02001647
Yurii Pavlovskyi54a31212019-05-14 21:04:48 +02001648/* Backlight ******************************************************************/
1649
Corentin Chary1d070f82011-02-26 10:20:36 +01001650static int read_backlight_power(struct asus_wmi *asus)
Corentin Charyb7187262011-02-06 13:28:39 +01001651{
AceLan Kao6e0044b2012-03-20 09:53:09 +01001652 int ret;
1653 if (asus->driver->quirks->store_backlight_power)
1654 ret = !asus->driver->panel_power;
1655 else
1656 ret = asus_wmi_get_devstate_simple(asus,
1657 ASUS_WMI_DEVID_BACKLIGHT);
Corentin Charyb7187262011-02-06 13:28:39 +01001658
1659 if (ret < 0)
1660 return ret;
1661
1662 return ret ? FB_BLANK_UNBLANK : FB_BLANK_POWERDOWN;
1663}
1664
Corentin Chary8fbea012011-02-26 10:20:37 +01001665static int read_brightness_max(struct asus_wmi *asus)
1666{
1667 u32 retval;
1668 int err;
1669
1670 err = asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_BRIGHTNESS, &retval);
1671
1672 if (err < 0)
1673 return err;
1674
1675 retval = retval & ASUS_WMI_DSTS_MAX_BRIGTH_MASK;
1676 retval >>= 8;
1677
1678 if (!retval)
1679 return -ENODEV;
1680
1681 return retval;
1682}
1683
Yong Wang3d7b1652010-04-11 09:27:54 +08001684static int read_brightness(struct backlight_device *bd)
1685{
Corentin Chary1d070f82011-02-26 10:20:36 +01001686 struct asus_wmi *asus = bl_get_data(bd);
Dan Carpenter0986f252011-03-15 10:06:23 +03001687 u32 retval;
1688 int err;
Yong Wang3d7b1652010-04-11 09:27:54 +08001689
Corentin Chary1d070f82011-02-26 10:20:36 +01001690 err = asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_BRIGHTNESS, &retval);
Yong Wang3d7b1652010-04-11 09:27:54 +08001691
Corentin Charyd33da3b2011-02-26 10:20:35 +01001692 if (err < 0)
1693 return err;
1694
1695 return retval & ASUS_WMI_DSTS_BRIGHTNESS_MASK;
Yong Wang3d7b1652010-04-11 09:27:54 +08001696}
1697
AceLan Kaoc87992d2012-03-20 09:53:08 +01001698static u32 get_scalar_command(struct backlight_device *bd)
1699{
1700 struct asus_wmi *asus = bl_get_data(bd);
1701 u32 ctrl_param = 0;
1702
1703 if ((asus->driver->brightness < bd->props.brightness) ||
1704 bd->props.brightness == bd->props.max_brightness)
1705 ctrl_param = 0x00008001;
1706 else if ((asus->driver->brightness > bd->props.brightness) ||
1707 bd->props.brightness == 0)
1708 ctrl_param = 0x00008000;
1709
1710 asus->driver->brightness = bd->props.brightness;
1711
1712 return ctrl_param;
1713}
1714
Yong Wang3d7b1652010-04-11 09:27:54 +08001715static int update_bl_status(struct backlight_device *bd)
1716{
Corentin Chary1d070f82011-02-26 10:20:36 +01001717 struct asus_wmi *asus = bl_get_data(bd);
Corentin Charydfed65d2010-11-29 08:14:12 +01001718 u32 ctrl_param;
AceLan Kao6e0044b2012-03-20 09:53:09 +01001719 int power, err = 0;
Corentin Charyb7187262011-02-06 13:28:39 +01001720
Corentin Chary1d070f82011-02-26 10:20:36 +01001721 power = read_backlight_power(asus);
Corentin Charyb7187262011-02-06 13:28:39 +01001722 if (power != -ENODEV && bd->props.power != power) {
1723 ctrl_param = !!(bd->props.power == FB_BLANK_UNBLANK);
Corentin Charyd33da3b2011-02-26 10:20:35 +01001724 err = asus_wmi_set_devstate(ASUS_WMI_DEVID_BACKLIGHT,
1725 ctrl_param, NULL);
AceLan Kao6e0044b2012-03-20 09:53:09 +01001726 if (asus->driver->quirks->store_backlight_power)
1727 asus->driver->panel_power = bd->props.power;
AceLan Kao6e0044b2012-03-20 09:53:09 +01001728
Corentin Charyade28ab2012-03-20 09:53:14 +01001729 /* When using scalar brightness, updating the brightness
1730 * will mess with the backlight power */
1731 if (asus->driver->quirks->scalar_panel_brightness)
1732 return err;
Corentin Charyb7187262011-02-06 13:28:39 +01001733 }
Corentin Charyade28ab2012-03-20 09:53:14 +01001734
1735 if (asus->driver->quirks->scalar_panel_brightness)
1736 ctrl_param = get_scalar_command(bd);
1737 else
1738 ctrl_param = bd->props.brightness;
1739
1740 err = asus_wmi_set_devstate(ASUS_WMI_DEVID_BRIGHTNESS,
1741 ctrl_param, NULL);
1742
Corentin Chary8fbea012011-02-26 10:20:37 +01001743 return err;
Yong Wang3d7b1652010-04-11 09:27:54 +08001744}
1745
Corentin Charye12e6d92011-02-26 10:20:31 +01001746static const struct backlight_ops asus_wmi_bl_ops = {
Yong Wang3d7b1652010-04-11 09:27:54 +08001747 .get_brightness = read_brightness,
1748 .update_status = update_bl_status,
1749};
1750
Corentin Charye12e6d92011-02-26 10:20:31 +01001751static int asus_wmi_backlight_notify(struct asus_wmi *asus, int code)
Yong Wang3d7b1652010-04-11 09:27:54 +08001752{
Corentin Charye12e6d92011-02-26 10:20:31 +01001753 struct backlight_device *bd = asus->backlight_device;
Yong Wang3d7b1652010-04-11 09:27:54 +08001754 int old = bd->props.brightness;
Daniel Mackb7670ed2010-05-19 12:37:01 +02001755 int new = old;
Yong Wang3d7b1652010-04-11 09:27:54 +08001756
1757 if (code >= NOTIFY_BRNUP_MIN && code <= NOTIFY_BRNUP_MAX)
1758 new = code - NOTIFY_BRNUP_MIN + 1;
1759 else if (code >= NOTIFY_BRNDOWN_MIN && code <= NOTIFY_BRNDOWN_MAX)
1760 new = code - NOTIFY_BRNDOWN_MIN;
1761
1762 bd->props.brightness = new;
1763 backlight_update_status(bd);
1764 backlight_force_update(bd, BACKLIGHT_UPDATE_HOTKEY);
1765
1766 return old;
1767}
1768
Corentin Charye12e6d92011-02-26 10:20:31 +01001769static int asus_wmi_backlight_init(struct asus_wmi *asus)
Yong Wang3d7b1652010-04-11 09:27:54 +08001770{
1771 struct backlight_device *bd;
1772 struct backlight_properties props;
Corentin Charyb7187262011-02-06 13:28:39 +01001773 int max;
1774 int power;
1775
Corentin Chary8fbea012011-02-26 10:20:37 +01001776 max = read_brightness_max(asus);
Hans de Goede86ac27352014-07-08 10:47:22 +02001777 if (max < 0)
Corentin Chary8fbea012011-02-26 10:20:37 +01001778 return max;
1779
1780 power = read_backlight_power(asus);
1781
Corentin Charyb7187262011-02-06 13:28:39 +01001782 if (power == -ENODEV)
1783 power = FB_BLANK_UNBLANK;
Corentin Chary8fbea012011-02-26 10:20:37 +01001784 else if (power < 0)
1785 return power;
Yong Wang3d7b1652010-04-11 09:27:54 +08001786
1787 memset(&props, 0, sizeof(struct backlight_properties));
Axel Lin60cfa092011-06-29 11:43:30 +08001788 props.type = BACKLIGHT_PLATFORM;
Corentin Charyb7187262011-02-06 13:28:39 +01001789 props.max_brightness = max;
Corentin Charye12e6d92011-02-26 10:20:31 +01001790 bd = backlight_device_register(asus->driver->name,
1791 &asus->platform_device->dev, asus,
1792 &asus_wmi_bl_ops, &props);
Yong Wang3d7b1652010-04-11 09:27:54 +08001793 if (IS_ERR(bd)) {
1794 pr_err("Could not register backlight device\n");
1795 return PTR_ERR(bd);
1796 }
1797
Corentin Charye12e6d92011-02-26 10:20:31 +01001798 asus->backlight_device = bd;
Yong Wang3d7b1652010-04-11 09:27:54 +08001799
AceLan Kao6e0044b2012-03-20 09:53:09 +01001800 if (asus->driver->quirks->store_backlight_power)
1801 asus->driver->panel_power = power;
1802
Yong Wang3d7b1652010-04-11 09:27:54 +08001803 bd->props.brightness = read_brightness(bd);
Corentin Charyb7187262011-02-06 13:28:39 +01001804 bd->props.power = power;
Yong Wang3d7b1652010-04-11 09:27:54 +08001805 backlight_update_status(bd);
1806
AceLan Kaoc87992d2012-03-20 09:53:08 +01001807 asus->driver->brightness = bd->props.brightness;
1808
Yong Wang3d7b1652010-04-11 09:27:54 +08001809 return 0;
1810}
1811
Corentin Charye12e6d92011-02-26 10:20:31 +01001812static void asus_wmi_backlight_exit(struct asus_wmi *asus)
Yong Wang3d7b1652010-04-11 09:27:54 +08001813{
Markus Elfring00981812014-11-24 20:30:29 +01001814 backlight_device_unregister(asus->backlight_device);
Yong Wang3d7b1652010-04-11 09:27:54 +08001815
Corentin Charye12e6d92011-02-26 10:20:31 +01001816 asus->backlight_device = NULL;
Yong Wang3d7b1652010-04-11 09:27:54 +08001817}
1818
AceLan Kaoa2a96f02012-10-03 11:26:31 +02001819static int is_display_toggle(int code)
1820{
1821 /* display toggle keys */
1822 if ((code >= 0x61 && code <= 0x67) ||
1823 (code >= 0x8c && code <= 0x93) ||
1824 (code >= 0xa0 && code <= 0xa7) ||
1825 (code >= 0xd0 && code <= 0xd5))
1826 return 1;
1827
1828 return 0;
1829}
1830
Yurii Pavlovskyi54a31212019-05-14 21:04:48 +02001831/* Fn-lock ********************************************************************/
1832
Chris Chiu487579b2019-04-18 14:46:48 +08001833static bool asus_wmi_has_fnlock_key(struct asus_wmi *asus)
1834{
1835 u32 result;
1836
1837 asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_FNLOCK, &result);
1838
1839 return (result & ASUS_WMI_DSTS_PRESENCE_BIT) &&
1840 !(result & ASUS_WMI_FNLOCK_BIOS_DISABLED);
1841}
1842
1843static void asus_wmi_fnlock_update(struct asus_wmi *asus)
1844{
1845 int mode = asus->fnlock_locked;
1846
1847 asus_wmi_set_devstate(ASUS_WMI_DEVID_FNLOCK, mode, NULL);
1848}
1849
Yurii Pavlovskyi54a31212019-05-14 21:04:48 +02001850/* WMI events *****************************************************************/
1851
Yurii Pavlovskyi8abd752b2019-05-14 21:01:24 +02001852static int asus_wmi_get_event_code(u32 value)
Yong Wang3d7b1652010-04-11 09:27:54 +08001853{
Yong Wang3d7b1652010-04-11 09:27:54 +08001854 struct acpi_buffer response = { ACPI_ALLOCATE_BUFFER, NULL };
1855 union acpi_object *obj;
1856 acpi_status status;
1857 int code;
Yong Wang3d7b1652010-04-11 09:27:54 +08001858
1859 status = wmi_get_event_data(value, &response);
Yurii Pavlovskyi8abd752b2019-05-14 21:01:24 +02001860 if (ACPI_FAILURE(status)) {
1861 pr_warn("Failed to get WMI notify code: %s\n",
1862 acpi_format_exception(status));
1863 return -EIO;
Yong Wang3d7b1652010-04-11 09:27:54 +08001864 }
1865
1866 obj = (union acpi_object *)response.pointer;
1867
Yurii Pavlovskyi8abd752b2019-05-14 21:01:24 +02001868 if (obj && obj->type == ACPI_TYPE_INTEGER)
1869 code = (int)(obj->integer.value & WMI_EVENT_MASK);
1870 else
1871 code = -EIO;
Yong Wang3d7b1652010-04-11 09:27:54 +08001872
Yurii Pavlovskyi8abd752b2019-05-14 21:01:24 +02001873 kfree(obj);
1874 return code;
1875}
1876
1877static void asus_wmi_handle_event_code(int code, struct asus_wmi *asus)
1878{
1879 int orig_code;
1880 unsigned int key_value = 1;
1881 bool autorelease = 1;
1882
Corentin Chary57ab7da2011-02-26 10:20:32 +01001883 orig_code = code;
Yong Wang3d7b1652010-04-11 09:27:54 +08001884
Seth Forsheec4453f62011-07-01 11:34:27 +02001885 if (asus->driver->key_filter) {
1886 asus->driver->key_filter(asus->driver, &code, &key_value,
1887 &autorelease);
1888 if (code == ASUS_WMI_KEY_IGNORE)
Yurii Pavlovskyi8abd752b2019-05-14 21:01:24 +02001889 return;
Seth Forsheec4453f62011-07-01 11:34:27 +02001890 }
1891
Corentin Chary57ab7da2011-02-26 10:20:32 +01001892 if (code >= NOTIFY_BRNUP_MIN && code <= NOTIFY_BRNUP_MAX)
Corentin Chary3ba03022012-11-29 09:12:38 +01001893 code = ASUS_WMI_BRN_UP;
Yurii Pavlovskyi8abd752b2019-05-14 21:01:24 +02001894 else if (code >= NOTIFY_BRNDOWN_MIN && code <= NOTIFY_BRNDOWN_MAX)
Corentin Chary3ba03022012-11-29 09:12:38 +01001895 code = ASUS_WMI_BRN_DOWN;
Yong Wang3d7b1652010-04-11 09:27:54 +08001896
Corentin Chary3ba03022012-11-29 09:12:38 +01001897 if (code == ASUS_WMI_BRN_DOWN || code == ASUS_WMI_BRN_UP) {
Hans de Goede62c4aa12015-06-16 16:27:58 +02001898 if (acpi_video_get_backlight_type() == acpi_backlight_vendor) {
Corentin Chary57ab7da2011-02-26 10:20:32 +01001899 asus_wmi_backlight_notify(asus, orig_code);
Yurii Pavlovskyi8abd752b2019-05-14 21:01:24 +02001900 return;
AceLan Kaoa2a96f02012-10-03 11:26:31 +02001901 }
AceLan Kaoa2a96f02012-10-03 11:26:31 +02001902 }
1903
Chris Chiudbb3d782018-06-20 22:46:44 +08001904 if (code == NOTIFY_KBD_BRTUP) {
Jian-Hong Pan29f6eb52018-10-22 18:00:04 +08001905 kbd_led_set_by_kbd(asus, asus->kbd_led_wk + 1);
Yurii Pavlovskyi8abd752b2019-05-14 21:01:24 +02001906 return;
Chris Chiudbb3d782018-06-20 22:46:44 +08001907 }
1908 if (code == NOTIFY_KBD_BRTDWN) {
Jian-Hong Pan29f6eb52018-10-22 18:00:04 +08001909 kbd_led_set_by_kbd(asus, asus->kbd_led_wk - 1);
Yurii Pavlovskyi8abd752b2019-05-14 21:01:24 +02001910 return;
Chris Chiudbb3d782018-06-20 22:46:44 +08001911 }
Chris Chiued99d292018-06-20 22:46:45 +08001912 if (code == NOTIFY_KBD_BRTTOGGLE) {
1913 if (asus->kbd_led_wk == asus->kbd_led.max_brightness)
Jian-Hong Pan29f6eb52018-10-22 18:00:04 +08001914 kbd_led_set_by_kbd(asus, 0);
Chris Chiued99d292018-06-20 22:46:45 +08001915 else
Jian-Hong Pan29f6eb52018-10-22 18:00:04 +08001916 kbd_led_set_by_kbd(asus, asus->kbd_led_wk + 1);
Yurii Pavlovskyi8abd752b2019-05-14 21:01:24 +02001917 return;
Chris Chiued99d292018-06-20 22:46:45 +08001918 }
Chris Chiudbb3d782018-06-20 22:46:44 +08001919
Chris Chiu487579b2019-04-18 14:46:48 +08001920 if (code == NOTIFY_FNLOCK_TOGGLE) {
1921 asus->fnlock_locked = !asus->fnlock_locked;
1922 asus_wmi_fnlock_update(asus);
Yurii Pavlovskyi8abd752b2019-05-14 21:01:24 +02001923 return;
Chris Chiu487579b2019-04-18 14:46:48 +08001924 }
1925
Daniel Drake9af93db2019-07-17 13:10:58 +08001926 if (asus->fan_boost_mode_available && code == NOTIFY_KBD_FBM) {
1927 fan_boost_mode_switch_next(asus);
Yurii Pavlovskyib096f622019-05-14 21:07:05 +02001928 return;
1929 }
1930
Yurii Pavlovskyi8abd752b2019-05-14 21:01:24 +02001931 if (is_display_toggle(code) && asus->driver->quirks->no_display_toggle)
1932 return;
AceLan Kaoa2a96f02012-10-03 11:26:31 +02001933
1934 if (!sparse_keymap_report_event(asus->inputdev, code,
1935 key_value, autorelease))
Corentin Chary57ab7da2011-02-26 10:20:32 +01001936 pr_info("Unknown key %x pressed\n", code);
Yong Wang3d7b1652010-04-11 09:27:54 +08001937}
1938
Yurii Pavlovskyi8abd752b2019-05-14 21:01:24 +02001939static void asus_wmi_notify(u32 value, void *context)
1940{
1941 struct asus_wmi *asus = context;
Yurii Pavlovskyi1a373d12019-05-14 21:02:09 +02001942 int code;
1943 int i;
Yurii Pavlovskyi8abd752b2019-05-14 21:01:24 +02001944
Yurii Pavlovskyi1a373d12019-05-14 21:02:09 +02001945 for (i = 0; i < WMI_EVENT_QUEUE_SIZE + 1; i++) {
1946 code = asus_wmi_get_event_code(value);
1947
1948 if (code < 0) {
1949 pr_warn("Failed to get notify code: %d\n", code);
1950 return;
1951 }
1952
1953 if (code == WMI_EVENT_QUEUE_END || code == WMI_EVENT_MASK)
1954 return;
1955
1956 asus_wmi_handle_event_code(code, asus);
1957
1958 /*
1959 * Double check that queue is present:
1960 * ATK (with queue) uses 0xff, ASUSWMI (without) 0xd2.
1961 */
1962 if (!asus->wmi_event_queue || value != WMI_EVENT_VALUE_ATK)
1963 return;
Yurii Pavlovskyi8abd752b2019-05-14 21:01:24 +02001964 }
1965
Yurii Pavlovskyi1a373d12019-05-14 21:02:09 +02001966 pr_warn("Failed to process event queue, last code: 0x%x\n", code);
1967}
1968
1969static int asus_wmi_notify_queue_flush(struct asus_wmi *asus)
1970{
1971 int code;
1972 int i;
1973
1974 for (i = 0; i < WMI_EVENT_QUEUE_SIZE + 1; i++) {
1975 code = asus_wmi_get_event_code(WMI_EVENT_VALUE_ATK);
1976
1977 if (code < 0) {
1978 pr_warn("Failed to get event during flush: %d\n", code);
1979 return code;
1980 }
1981
1982 if (code == WMI_EVENT_QUEUE_END || code == WMI_EVENT_MASK)
1983 return 0;
1984 }
1985
1986 pr_warn("Failed to flush event queue\n");
1987 return -EIO;
Yong Wangee027e42010-03-21 10:26:34 +08001988}
1989
Yurii Pavlovskyi54a31212019-05-14 21:04:48 +02001990/* Sysfs **********************************************************************/
1991
Corentin Chary9e1565b2011-02-06 13:28:36 +01001992static int parse_arg(const char *buf, unsigned long count, int *val)
1993{
1994 if (!count)
1995 return 0;
1996 if (sscanf(buf, "%i", val) != 1)
1997 return -EINVAL;
1998 return count;
1999}
2000
Corentin Chary1d070f82011-02-26 10:20:36 +01002001static ssize_t store_sys_wmi(struct asus_wmi *asus, int devid,
2002 const char *buf, size_t count)
Corentin Chary9e1565b2011-02-06 13:28:36 +01002003{
Corentin Chary9e1565b2011-02-06 13:28:36 +01002004 u32 retval;
Corentin Charyd33da3b2011-02-26 10:20:35 +01002005 int rv, err, value;
Corentin Chary9e1565b2011-02-06 13:28:36 +01002006
Corentin Chary1d070f82011-02-26 10:20:36 +01002007 value = asus_wmi_get_devstate_simple(asus, devid);
Dan Carpenterb8298342015-11-11 01:18:16 +03002008 if (value < 0)
Corentin Chary9e1565b2011-02-06 13:28:36 +01002009 return value;
2010
2011 rv = parse_arg(buf, count, &value);
Corentin Charyd33da3b2011-02-26 10:20:35 +01002012 err = asus_wmi_set_devstate(devid, value, &retval);
Corentin Chary9e1565b2011-02-06 13:28:36 +01002013
Corentin Charyd33da3b2011-02-26 10:20:35 +01002014 if (err < 0)
2015 return err;
2016
Corentin Chary9e1565b2011-02-06 13:28:36 +01002017 return rv;
2018}
2019
Corentin Chary1d070f82011-02-26 10:20:36 +01002020static ssize_t show_sys_wmi(struct asus_wmi *asus, int devid, char *buf)
Corentin Chary9e1565b2011-02-06 13:28:36 +01002021{
Corentin Chary1d070f82011-02-26 10:20:36 +01002022 int value = asus_wmi_get_devstate_simple(asus, devid);
Corentin Chary9e1565b2011-02-06 13:28:36 +01002023
2024 if (value < 0)
2025 return value;
2026
2027 return sprintf(buf, "%d\n", value);
2028}
2029
Corentin Charye12e6d92011-02-26 10:20:31 +01002030#define ASUS_WMI_CREATE_DEVICE_ATTR(_name, _mode, _cm) \
Corentin Chary9e1565b2011-02-06 13:28:36 +01002031 static ssize_t show_##_name(struct device *dev, \
2032 struct device_attribute *attr, \
2033 char *buf) \
2034 { \
Corentin Chary1d070f82011-02-26 10:20:36 +01002035 struct asus_wmi *asus = dev_get_drvdata(dev); \
2036 \
2037 return show_sys_wmi(asus, _cm, buf); \
Corentin Chary9e1565b2011-02-06 13:28:36 +01002038 } \
2039 static ssize_t store_##_name(struct device *dev, \
2040 struct device_attribute *attr, \
2041 const char *buf, size_t count) \
2042 { \
Corentin Chary1d070f82011-02-26 10:20:36 +01002043 struct asus_wmi *asus = dev_get_drvdata(dev); \
2044 \
2045 return store_sys_wmi(asus, _cm, buf, count); \
Corentin Chary9e1565b2011-02-06 13:28:36 +01002046 } \
2047 static struct device_attribute dev_attr_##_name = { \
2048 .attr = { \
2049 .name = __stringify(_name), \
2050 .mode = _mode }, \
2051 .show = show_##_name, \
2052 .store = store_##_name, \
2053 }
2054
Corentin Charye12e6d92011-02-26 10:20:31 +01002055ASUS_WMI_CREATE_DEVICE_ATTR(touchpad, 0644, ASUS_WMI_DEVID_TOUCHPAD);
2056ASUS_WMI_CREATE_DEVICE_ATTR(camera, 0644, ASUS_WMI_DEVID_CAMERA);
2057ASUS_WMI_CREATE_DEVICE_ATTR(cardr, 0644, ASUS_WMI_DEVID_CARDREADER);
AceLan Kaoc0b91b62012-06-13 09:32:07 +02002058ASUS_WMI_CREATE_DEVICE_ATTR(lid_resume, 0644, ASUS_WMI_DEVID_LID_RESUME);
Oleksij Rempelaca234f2016-04-01 13:35:21 +02002059ASUS_WMI_CREATE_DEVICE_ATTR(als_enable, 0644, ASUS_WMI_DEVID_ALS_ENABLE);
Corentin Chary9e1565b2011-02-06 13:28:36 +01002060
Jérémy Lefaurea8fe3422017-04-21 22:19:45 -04002061static ssize_t cpufv_store(struct device *dev, struct device_attribute *attr,
Dmitry Torokhov67fa38e2010-11-03 11:14:01 -07002062 const char *buf, size_t count)
Chris Bagwell7f80d732010-10-11 18:47:18 -05002063{
Corentin Chary3df5fda2011-07-01 11:34:38 +02002064 int value, rv;
Chris Bagwell7f80d732010-10-11 18:47:18 -05002065
2066 if (!count || sscanf(buf, "%i", &value) != 1)
2067 return -EINVAL;
2068 if (value < 0 || value > 2)
2069 return -EINVAL;
2070
Corentin Chary3df5fda2011-07-01 11:34:38 +02002071 rv = asus_wmi_evaluate_method(ASUS_WMI_METHODID_CFVS, value, 0, NULL);
2072 if (rv < 0)
2073 return rv;
2074
2075 return count;
Chris Bagwell7f80d732010-10-11 18:47:18 -05002076}
2077
Jérémy Lefaurea8fe3422017-04-21 22:19:45 -04002078static DEVICE_ATTR_WO(cpufv);
Chris Bagwell7f80d732010-10-11 18:47:18 -05002079
Kristian Klausend507a542019-08-05 21:23:05 +02002080
2081static ssize_t charge_threshold_store(struct device *dev,
2082 struct device_attribute *attr,
2083 const char *buf, size_t count)
2084{
2085 struct asus_wmi *asus = dev_get_drvdata(dev);
2086 int value, ret, rv;
2087
2088 ret = kstrtouint(buf, 10, &value);
2089
2090 if (!count || ret != 0)
2091 return -EINVAL;
2092 if (value < 0 || value > 100)
2093 return -EINVAL;
2094
2095 asus_wmi_set_devstate(ASUS_WMI_CHARGE_THRESHOLD, value, &rv);
2096
2097 if (rv != 1)
2098 return -EIO;
2099
2100 /* There isn't any method in the DSDT to read the threshold, so we
2101 * save the threshold.
2102 */
2103 asus->charge_threshold = value;
2104 return count;
2105}
2106
2107static ssize_t charge_threshold_show(struct device *dev,
2108 struct device_attribute *attr, char *buf)
2109{
2110 struct asus_wmi *asus = dev_get_drvdata(dev);
2111
2112 return sprintf(buf, "%d\n", asus->charge_threshold);
2113}
2114
2115static DEVICE_ATTR_RW(charge_threshold);
2116
Corentin Chary4e37b422010-11-29 08:14:08 +01002117static struct attribute *platform_attributes[] = {
2118 &dev_attr_cpufv.attr,
Corentin Chary9e1565b2011-02-06 13:28:36 +01002119 &dev_attr_camera.attr,
2120 &dev_attr_cardr.attr,
Corentin Chary4615bb62011-02-06 13:28:42 +01002121 &dev_attr_touchpad.attr,
AceLan Kaoc0b91b62012-06-13 09:32:07 +02002122 &dev_attr_lid_resume.attr,
Oleksij Rempelaca234f2016-04-01 13:35:21 +02002123 &dev_attr_als_enable.attr,
Daniel Drake9af93db2019-07-17 13:10:58 +08002124 &dev_attr_fan_boost_mode.attr,
Kristian Klausend507a542019-08-05 21:23:05 +02002125 &dev_attr_charge_threshold.attr,
Corentin Chary4e37b422010-11-29 08:14:08 +01002126 NULL
2127};
2128
Al Viro587a1f12011-07-23 23:11:19 -04002129static umode_t asus_sysfs_is_visible(struct kobject *kobj,
Corentin Charye12e6d92011-02-26 10:20:31 +01002130 struct attribute *attr, int idx)
Corentin Chary9e1565b2011-02-06 13:28:36 +01002131{
Corentin Chary1d070f82011-02-26 10:20:36 +01002132 struct device *dev = container_of(kobj, struct device, kobj);
Wolfram Sangd605ca22018-04-19 16:06:10 +02002133 struct asus_wmi *asus = dev_get_drvdata(dev);
Corentin Chary1d070f82011-02-26 10:20:36 +01002134 bool ok = true;
Corentin Chary9e1565b2011-02-06 13:28:36 +01002135 int devid = -1;
2136
2137 if (attr == &dev_attr_camera.attr)
Corentin Charye12e6d92011-02-26 10:20:31 +01002138 devid = ASUS_WMI_DEVID_CAMERA;
Corentin Chary9e1565b2011-02-06 13:28:36 +01002139 else if (attr == &dev_attr_cardr.attr)
Corentin Charye12e6d92011-02-26 10:20:31 +01002140 devid = ASUS_WMI_DEVID_CARDREADER;
Corentin Chary4615bb62011-02-06 13:28:42 +01002141 else if (attr == &dev_attr_touchpad.attr)
Corentin Charye12e6d92011-02-26 10:20:31 +01002142 devid = ASUS_WMI_DEVID_TOUCHPAD;
AceLan Kaoc0b91b62012-06-13 09:32:07 +02002143 else if (attr == &dev_attr_lid_resume.attr)
2144 devid = ASUS_WMI_DEVID_LID_RESUME;
Oleksij Rempelaca234f2016-04-01 13:35:21 +02002145 else if (attr == &dev_attr_als_enable.attr)
2146 devid = ASUS_WMI_DEVID_ALS_ENABLE;
Daniel Drake9af93db2019-07-17 13:10:58 +08002147 else if (attr == &dev_attr_fan_boost_mode.attr)
2148 ok = asus->fan_boost_mode_available;
Kristian Klausend507a542019-08-05 21:23:05 +02002149 else if (attr == &dev_attr_charge_threshold.attr)
2150 devid = ASUS_WMI_CHARGE_THRESHOLD;
Corentin Chary9e1565b2011-02-06 13:28:36 +01002151
2152 if (devid != -1)
Corentin Chary1d070f82011-02-26 10:20:36 +01002153 ok = !(asus_wmi_get_devstate_simple(asus, devid) < 0);
Corentin Chary9e1565b2011-02-06 13:28:36 +01002154
Corentin Chary1d070f82011-02-26 10:20:36 +01002155 return ok ? attr->mode : 0;
Corentin Chary9e1565b2011-02-06 13:28:36 +01002156}
2157
Arvind Yadave90d9ba2017-07-11 16:18:19 +05302158static const struct attribute_group platform_attribute_group = {
Corentin Charye12e6d92011-02-26 10:20:31 +01002159 .is_visible = asus_sysfs_is_visible,
2160 .attrs = platform_attributes
Corentin Chary4e37b422010-11-29 08:14:08 +01002161};
2162
Corentin Charye12e6d92011-02-26 10:20:31 +01002163static void asus_wmi_sysfs_exit(struct platform_device *device)
Chris Bagwell7f80d732010-10-11 18:47:18 -05002164{
Corentin Chary4e37b422010-11-29 08:14:08 +01002165 sysfs_remove_group(&device->dev.kobj, &platform_attribute_group);
Chris Bagwell7f80d732010-10-11 18:47:18 -05002166}
2167
Corentin Charye12e6d92011-02-26 10:20:31 +01002168static int asus_wmi_sysfs_init(struct platform_device *device)
Chris Bagwell7f80d732010-10-11 18:47:18 -05002169{
Corentin Chary4e37b422010-11-29 08:14:08 +01002170 return sysfs_create_group(&device->dev.kobj, &platform_attribute_group);
Chris Bagwell7f80d732010-10-11 18:47:18 -05002171}
2172
Yurii Pavlovskyi54a31212019-05-14 21:04:48 +02002173/* Platform device ************************************************************/
2174
Joe Perches39ddf3b2011-03-29 15:21:32 -07002175static int asus_wmi_platform_init(struct asus_wmi *asus)
Corentin Chary27c136c2010-11-29 08:14:05 +01002176{
Yurii Pavlovskyie0668f22019-05-14 21:00:31 +02002177 struct device *dev = &asus->platform_device->dev;
2178 char *wmi_uid;
Corentin Chary46dbca82011-02-26 10:20:38 +01002179 int rv;
2180
2181 /* INIT enable hotkeys on some models */
2182 if (!asus_wmi_evaluate_method(ASUS_WMI_METHODID_INIT, 0, 0, &rv))
vic0ed60652013-05-22 21:32:10 +03002183 pr_info("Initialization: %#x\n", rv);
Corentin Chary46dbca82011-02-26 10:20:38 +01002184
2185 /* We don't know yet what to do with this version... */
2186 if (!asus_wmi_evaluate_method(ASUS_WMI_METHODID_SPEC, 0, 0x9, &rv)) {
vic0ed60652013-05-22 21:32:10 +03002187 pr_info("BIOS WMI version: %d.%d\n", rv >> 16, rv & 0xFF);
Corentin Chary46dbca82011-02-26 10:20:38 +01002188 asus->spec = rv;
2189 }
2190
2191 /*
2192 * The SFUN method probably allows the original driver to get the list
2193 * of features supported by a given model. For now, 0x0100 or 0x0800
2194 * bit signifies that the laptop is equipped with a Wi-Fi MiniPCI card.
2195 * The significance of others is yet to be found.
2196 */
2197 if (!asus_wmi_evaluate_method(ASUS_WMI_METHODID_SFUN, 0, 0, &rv)) {
vic0ed60652013-05-22 21:32:10 +03002198 pr_info("SFUN value: %#x\n", rv);
Corentin Chary46dbca82011-02-26 10:20:38 +01002199 asus->sfun = rv;
2200 }
2201
Corentin Chary1d070f82011-02-26 10:20:36 +01002202 /*
2203 * Eee PC and Notebooks seems to have different method_id for DSTS,
2204 * but it may also be related to the BIOS's SPEC.
2205 * Note, on most Eeepc, there is no way to check if a method exist
2206 * or note, while on notebooks, they returns 0xFFFFFFFE on failure,
2207 * but once again, SPEC may probably be used for that kind of things.
Yurii Pavlovskyie0668f22019-05-14 21:00:31 +02002208 *
2209 * Additionally at least TUF Gaming series laptops return nothing for
2210 * unknown methods, so the detection in this way is not possible.
2211 *
2212 * There is strong indication that only ACPI WMI devices that have _UID
2213 * equal to "ASUSWMI" use DCTS whereas those with "ATK" use DSTS.
Corentin Chary1d070f82011-02-26 10:20:36 +01002214 */
Yurii Pavlovskyie0668f22019-05-14 21:00:31 +02002215 wmi_uid = wmi_get_acpi_device_uid(ASUS_WMI_MGMT_GUID);
2216 if (!wmi_uid)
2217 return -ENODEV;
2218
2219 if (!strcmp(wmi_uid, ASUS_ACPI_UID_ASUSWMI)) {
2220 dev_info(dev, "Detected ASUSWMI, use DCTS\n");
2221 asus->dsts_id = ASUS_WMI_METHODID_DCTS;
2222 } else {
2223 dev_info(dev, "Detected %s, not ASUSWMI, use DSTS\n", wmi_uid);
Corentin Chary1d070f82011-02-26 10:20:36 +01002224 asus->dsts_id = ASUS_WMI_METHODID_DSTS;
Yurii Pavlovskyie0668f22019-05-14 21:00:31 +02002225 }
Corentin Chary1d070f82011-02-26 10:20:36 +01002226
Yurii Pavlovskyi1a373d12019-05-14 21:02:09 +02002227 /*
2228 * Some devices can have multiple event codes stored in a queue before
2229 * the module load if it was unloaded intermittently after calling
2230 * the INIT method (enables event handling). The WMI notify handler is
2231 * expected to retrieve all event codes until a retrieved code equals
2232 * queue end marker (One or Ones). Old codes are flushed from the queue
2233 * upon module load. Not enabling this when it should be has minimal
2234 * visible impact so fall back if anything goes wrong.
2235 */
2236 wmi_uid = wmi_get_acpi_device_uid(asus->driver->event_guid);
2237 if (wmi_uid && !strcmp(wmi_uid, ASUS_ACPI_UID_ATK)) {
2238 dev_info(dev, "Detected ATK, enable event queue\n");
2239
2240 if (!asus_wmi_notify_queue_flush(asus))
2241 asus->wmi_event_queue = true;
2242 }
Corentin Chary1d070f82011-02-26 10:20:36 +01002243
Corentin Charyfddbfed2011-07-01 11:34:39 +02002244 /* CWAP allow to define the behavior of the Fn+F2 key,
2245 * this method doesn't seems to be present on Eee PCs */
Corentin Chary6a2bccc2012-03-20 09:53:10 +01002246 if (asus->driver->quirks->wapf >= 0)
Corentin Charyfddbfed2011-07-01 11:34:39 +02002247 asus_wmi_set_devstate(ASUS_WMI_DEVID_CWAP,
Corentin Chary6a2bccc2012-03-20 09:53:10 +01002248 asus->driver->quirks->wapf, NULL);
Corentin Charyfddbfed2011-07-01 11:34:39 +02002249
Yurii Pavlovskyib096f622019-05-14 21:07:05 +02002250 return 0;
Corentin Chary27c136c2010-11-29 08:14:05 +01002251}
2252
Yurii Pavlovskyi54a31212019-05-14 21:04:48 +02002253/* debugfs ********************************************************************/
Corentin Chary27c136c2010-11-29 08:14:05 +01002254
Corentin Charye12e6d92011-02-26 10:20:31 +01002255struct asus_wmi_debugfs_node {
2256 struct asus_wmi *asus;
Corentin Chary8c1b2d82010-11-29 08:14:09 +01002257 char *name;
Corentin Charye12e6d92011-02-26 10:20:31 +01002258 int (*show) (struct seq_file *m, void *data);
Corentin Chary8c1b2d82010-11-29 08:14:09 +01002259};
2260
2261static int show_dsts(struct seq_file *m, void *data)
2262{
Corentin Charye12e6d92011-02-26 10:20:31 +01002263 struct asus_wmi *asus = m->private;
Corentin Charyd33da3b2011-02-26 10:20:35 +01002264 int err;
Corentin Chary8c1b2d82010-11-29 08:14:09 +01002265 u32 retval = -1;
2266
Corentin Chary1d070f82011-02-26 10:20:36 +01002267 err = asus_wmi_get_devstate(asus, asus->debug.dev_id, &retval);
Corentin Chary8c1b2d82010-11-29 08:14:09 +01002268
Corentin Charyd33da3b2011-02-26 10:20:35 +01002269 if (err < 0)
2270 return err;
Corentin Chary8c1b2d82010-11-29 08:14:09 +01002271
Corentin Charyef343492011-02-26 10:20:39 +01002272 seq_printf(m, "DSTS(%#x) = %#x\n", asus->debug.dev_id, retval);
Corentin Chary8c1b2d82010-11-29 08:14:09 +01002273
2274 return 0;
2275}
2276
2277static int show_devs(struct seq_file *m, void *data)
2278{
Corentin Charye12e6d92011-02-26 10:20:31 +01002279 struct asus_wmi *asus = m->private;
Corentin Charyd33da3b2011-02-26 10:20:35 +01002280 int err;
Corentin Chary8c1b2d82010-11-29 08:14:09 +01002281 u32 retval = -1;
2282
Corentin Charyd33da3b2011-02-26 10:20:35 +01002283 err = asus_wmi_set_devstate(asus->debug.dev_id, asus->debug.ctrl_param,
2284 &retval);
2285
2286 if (err < 0)
2287 return err;
Corentin Chary8c1b2d82010-11-29 08:14:09 +01002288
Corentin Charyef343492011-02-26 10:20:39 +01002289 seq_printf(m, "DEVS(%#x, %#x) = %#x\n", asus->debug.dev_id,
Corentin Charye12e6d92011-02-26 10:20:31 +01002290 asus->debug.ctrl_param, retval);
Corentin Chary8c1b2d82010-11-29 08:14:09 +01002291
2292 return 0;
2293}
2294
Corentin Charyef343492011-02-26 10:20:39 +01002295static int show_call(struct seq_file *m, void *data)
2296{
2297 struct asus_wmi *asus = m->private;
2298 struct bios_args args = {
2299 .arg0 = asus->debug.dev_id,
2300 .arg1 = asus->debug.ctrl_param,
2301 };
2302 struct acpi_buffer input = { (acpi_size) sizeof(args), &args };
2303 struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER, NULL };
2304 union acpi_object *obj;
2305 acpi_status status;
2306
2307 status = wmi_evaluate_method(ASUS_WMI_MGMT_GUID,
Pali Rohár0fe57262017-08-12 09:44:16 +02002308 0, asus->debug.method_id,
Corentin Charyef343492011-02-26 10:20:39 +01002309 &input, &output);
2310
2311 if (ACPI_FAILURE(status))
2312 return -EIO;
2313
2314 obj = (union acpi_object *)output.pointer;
2315 if (obj && obj->type == ACPI_TYPE_INTEGER)
2316 seq_printf(m, "%#x(%#x, %#x) = %#x\n", asus->debug.method_id,
2317 asus->debug.dev_id, asus->debug.ctrl_param,
2318 (u32) obj->integer.value);
2319 else
2320 seq_printf(m, "%#x(%#x, %#x) = t:%d\n", asus->debug.method_id,
2321 asus->debug.dev_id, asus->debug.ctrl_param,
Dan Carpentera1d60862011-03-15 10:07:37 +03002322 obj ? obj->type : -1);
Corentin Charyef343492011-02-26 10:20:39 +01002323
2324 kfree(obj);
2325
2326 return 0;
2327}
2328
Corentin Charye12e6d92011-02-26 10:20:31 +01002329static struct asus_wmi_debugfs_node asus_wmi_debug_files[] = {
2330 {NULL, "devs", show_devs},
2331 {NULL, "dsts", show_dsts},
Corentin Charyef343492011-02-26 10:20:39 +01002332 {NULL, "call", show_call},
Corentin Chary8c1b2d82010-11-29 08:14:09 +01002333};
2334
Corentin Charye12e6d92011-02-26 10:20:31 +01002335static int asus_wmi_debugfs_open(struct inode *inode, struct file *file)
Corentin Chary8c1b2d82010-11-29 08:14:09 +01002336{
Corentin Charye12e6d92011-02-26 10:20:31 +01002337 struct asus_wmi_debugfs_node *node = inode->i_private;
Corentin Chary8c1b2d82010-11-29 08:14:09 +01002338
Corentin Charye12e6d92011-02-26 10:20:31 +01002339 return single_open(file, node->show, node->asus);
Corentin Chary8c1b2d82010-11-29 08:14:09 +01002340}
2341
Corentin Charye12e6d92011-02-26 10:20:31 +01002342static const struct file_operations asus_wmi_debugfs_io_ops = {
Corentin Chary8c1b2d82010-11-29 08:14:09 +01002343 .owner = THIS_MODULE,
Corentin Charye12e6d92011-02-26 10:20:31 +01002344 .open = asus_wmi_debugfs_open,
Corentin Chary8c1b2d82010-11-29 08:14:09 +01002345 .read = seq_read,
2346 .llseek = seq_lseek,
2347 .release = single_release,
2348};
2349
Corentin Charye12e6d92011-02-26 10:20:31 +01002350static void asus_wmi_debugfs_exit(struct asus_wmi *asus)
Corentin Chary8c1b2d82010-11-29 08:14:09 +01002351{
Corentin Charye12e6d92011-02-26 10:20:31 +01002352 debugfs_remove_recursive(asus->debug.root);
Corentin Chary8c1b2d82010-11-29 08:14:09 +01002353}
2354
Greg Kroah-Hartmand2785d32019-06-12 14:12:52 +02002355static void asus_wmi_debugfs_init(struct asus_wmi *asus)
Corentin Chary8c1b2d82010-11-29 08:14:09 +01002356{
Corentin Chary8c1b2d82010-11-29 08:14:09 +01002357 int i;
2358
Corentin Charye12e6d92011-02-26 10:20:31 +01002359 asus->debug.root = debugfs_create_dir(asus->driver->name, NULL);
Corentin Chary8c1b2d82010-11-29 08:14:09 +01002360
Greg Kroah-Hartmand2785d32019-06-12 14:12:52 +02002361 debugfs_create_x32("method_id", S_IRUGO | S_IWUSR, asus->debug.root,
2362 &asus->debug.method_id);
Corentin Charyef343492011-02-26 10:20:39 +01002363
Greg Kroah-Hartmand2785d32019-06-12 14:12:52 +02002364 debugfs_create_x32("dev_id", S_IRUGO | S_IWUSR, asus->debug.root,
2365 &asus->debug.dev_id);
Corentin Chary8c1b2d82010-11-29 08:14:09 +01002366
Greg Kroah-Hartmand2785d32019-06-12 14:12:52 +02002367 debugfs_create_x32("ctrl_param", S_IRUGO | S_IWUSR, asus->debug.root,
2368 &asus->debug.ctrl_param);
Corentin Chary8c1b2d82010-11-29 08:14:09 +01002369
Corentin Charye12e6d92011-02-26 10:20:31 +01002370 for (i = 0; i < ARRAY_SIZE(asus_wmi_debug_files); i++) {
2371 struct asus_wmi_debugfs_node *node = &asus_wmi_debug_files[i];
Corentin Chary8c1b2d82010-11-29 08:14:09 +01002372
Corentin Charye12e6d92011-02-26 10:20:31 +01002373 node->asus = asus;
Greg Kroah-Hartmand2785d32019-06-12 14:12:52 +02002374 debugfs_create_file(node->name, S_IFREG | S_IRUGO,
2375 asus->debug.root, node,
2376 &asus_wmi_debugfs_io_ops);
Corentin Chary8c1b2d82010-11-29 08:14:09 +01002377 }
Corentin Chary8c1b2d82010-11-29 08:14:09 +01002378}
2379
Yurii Pavlovskyi54a31212019-05-14 21:04:48 +02002380/* Init / exit ****************************************************************/
Kast Bernd53e755c2015-05-13 16:24:16 +02002381
Corentin Charye12e6d92011-02-26 10:20:31 +01002382static int asus_wmi_add(struct platform_device *pdev)
Corentin Charyafa7c882011-02-06 13:28:28 +01002383{
Corentin Charye12e6d92011-02-26 10:20:31 +01002384 struct platform_driver *pdrv = to_platform_driver(pdev->dev.driver);
2385 struct asus_wmi_driver *wdrv = to_asus_wmi_driver(pdrv);
2386 struct asus_wmi *asus;
Hans de Goeded8c66f62014-07-08 10:47:21 +02002387 const char *chassis_type;
Yong Wangee027e42010-03-21 10:26:34 +08002388 acpi_status status;
Corentin Chary27c136c2010-11-29 08:14:05 +01002389 int err;
AceLan Kaoa50bd122012-07-26 17:13:31 +08002390 u32 result;
Yong Wangee027e42010-03-21 10:26:34 +08002391
Corentin Charye12e6d92011-02-26 10:20:31 +01002392 asus = kzalloc(sizeof(struct asus_wmi), GFP_KERNEL);
2393 if (!asus)
Corentin Charya04ce292011-02-06 13:28:33 +01002394 return -ENOMEM;
2395
Corentin Charye12e6d92011-02-26 10:20:31 +01002396 asus->driver = wdrv;
2397 asus->platform_device = pdev;
2398 wdrv->platform_device = pdev;
2399 platform_set_drvdata(asus->platform_device, asus);
Corentin Chary27c136c2010-11-29 08:14:05 +01002400
AceLan Kaoc87992d2012-03-20 09:53:08 +01002401 if (wdrv->detect_quirks)
2402 wdrv->detect_quirks(asus->driver);
Corentin Charyafa7c882011-02-06 13:28:28 +01002403
Corentin Charye12e6d92011-02-26 10:20:31 +01002404 err = asus_wmi_platform_init(asus);
Corentin Chary27c136c2010-11-29 08:14:05 +01002405 if (err)
2406 goto fail_platform;
Yong Wang45f2c692010-04-11 09:27:19 +08002407
Daniel Drake9af93db2019-07-17 13:10:58 +08002408 err = fan_boost_mode_check_present(asus);
Yurii Pavlovskyib096f622019-05-14 21:07:05 +02002409 if (err)
Daniel Drake9af93db2019-07-17 13:10:58 +08002410 goto fail_fan_boost_mode;
Yurii Pavlovskyib096f622019-05-14 21:07:05 +02002411
2412 err = asus_wmi_sysfs_init(asus->platform_device);
2413 if (err)
2414 goto fail_sysfs;
2415
Corentin Charye12e6d92011-02-26 10:20:31 +01002416 err = asus_wmi_input_init(asus);
Yong Wang45f2c692010-04-11 09:27:19 +08002417 if (err)
Corentin Chary27c136c2010-11-29 08:14:05 +01002418 goto fail_input;
Yong Wang3d7b1652010-04-11 09:27:54 +08002419
Kast Bernd53e755c2015-05-13 16:24:16 +02002420 err = asus_wmi_fan_init(asus); /* probably no problems on error */
Kast Bernd53e755c2015-05-13 16:24:16 +02002421
Corentin Charye07babd2011-02-26 10:20:42 +01002422 err = asus_wmi_hwmon_init(asus);
2423 if (err)
2424 goto fail_hwmon;
2425
Corentin Charye12e6d92011-02-26 10:20:31 +01002426 err = asus_wmi_led_init(asus);
Corentin Chary084fca62010-11-29 08:14:06 +01002427 if (err)
2428 goto fail_leds;
2429
João Paulo Rechi Vita71050ae2017-02-20 14:50:22 -05002430 asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_WLAN, &result);
2431 if (result & (ASUS_WMI_DSTS_PRESENCE_BIT | ASUS_WMI_DSTS_USER_BIT))
2432 asus->driver->wlan_ctrl_by_user = 1;
2433
João Paulo Rechi Vitad1c4e9b2017-02-20 14:50:23 -05002434 if (!(asus->driver->wlan_ctrl_by_user && ashs_present())) {
João Paulo Rechi Vitaa977e592016-06-13 16:57:31 -04002435 err = asus_wmi_rfkill_init(asus);
2436 if (err)
2437 goto fail_rfkill;
2438 }
Corentin Charyba48fdb2010-11-29 08:14:07 +01002439
Oleksij Rempele9b61512017-04-28 16:19:49 +02002440 if (asus->driver->quirks->wmi_force_als_set)
2441 asus_wmi_set_als();
2442
Hans de Goeded8c66f62014-07-08 10:47:21 +02002443 /* Some Asus desktop boards export an acpi-video backlight interface,
2444 stop this from showing up */
2445 chassis_type = dmi_get_system_info(DMI_CHASSIS_TYPE);
2446 if (chassis_type && !strcmp(chassis_type, "3"))
Hans de Goede62c4aa12015-06-16 16:27:58 +02002447 acpi_video_set_dmi_backlight_type(acpi_backlight_vendor);
2448
AceLan Kao272c77d2012-06-13 09:32:06 +02002449 if (asus->driver->quirks->wmi_backlight_power)
Hans de Goede62c4aa12015-06-16 16:27:58 +02002450 acpi_video_set_dmi_backlight_type(acpi_backlight_vendor);
2451
zino lin999d4372016-08-28 16:12:06 +08002452 if (asus->driver->quirks->wmi_backlight_native)
2453 acpi_video_set_dmi_backlight_type(acpi_backlight_native);
2454
Kai-Chuan Hsieh8023eff2016-09-01 23:55:55 +08002455 if (asus->driver->quirks->xusb2pr)
2456 asus_wmi_set_xusb2pr(asus);
2457
Hans de Goede62c4aa12015-06-16 16:27:58 +02002458 if (acpi_video_get_backlight_type() == acpi_backlight_vendor) {
Corentin Charye12e6d92011-02-26 10:20:31 +01002459 err = asus_wmi_backlight_init(asus);
Corentin Charyb7187262011-02-06 13:28:39 +01002460 if (err && err != -ENODEV)
Corentin Chary27c136c2010-11-29 08:14:05 +01002461 goto fail_backlight;
Hans de Goede401fee82019-06-12 09:02:02 +02002462 } else if (asus->driver->quirks->wmi_backlight_set_devstate)
João Paulo Rechi Vita78f3ac72018-10-31 17:21:26 -07002463 err = asus_wmi_set_devstate(ASUS_WMI_DEVID_BACKLIGHT, 2, NULL);
Yong Wang45f2c692010-04-11 09:27:19 +08002464
Chris Chiu487579b2019-04-18 14:46:48 +08002465 if (asus_wmi_has_fnlock_key(asus)) {
2466 asus->fnlock_locked = true;
2467 asus_wmi_fnlock_update(asus);
2468 }
2469
Corentin Charye12e6d92011-02-26 10:20:31 +01002470 status = wmi_install_notify_handler(asus->driver->event_guid,
2471 asus_wmi_notify, asus);
Yong Wang45f2c692010-04-11 09:27:19 +08002472 if (ACPI_FAILURE(status)) {
Corentin Charye12e6d92011-02-26 10:20:31 +01002473 pr_err("Unable to register notify handler - %d\n", status);
Yong Wang45f2c692010-04-11 09:27:19 +08002474 err = -ENODEV;
Corentin Chary27c136c2010-11-29 08:14:05 +01002475 goto fail_wmi_handler;
Yong Wang45f2c692010-04-11 09:27:19 +08002476 }
2477
Greg Kroah-Hartmand2785d32019-06-12 14:12:52 +02002478 asus_wmi_debugfs_init(asus);
Kristian Klausend507a542019-08-05 21:23:05 +02002479 /* The charge threshold is only reset when the system is power cycled,
2480 * and we can't get the current threshold so let set it to 100% on
2481 * module load.
2482 */
2483 asus_wmi_set_devstate(ASUS_WMI_CHARGE_THRESHOLD, 100, NULL);
2484 asus->charge_threshold = 100;
Corentin Chary8c1b2d82010-11-29 08:14:09 +01002485
Corentin Charya04ce292011-02-06 13:28:33 +01002486 return 0;
Yong Wang45f2c692010-04-11 09:27:19 +08002487
Corentin Chary27c136c2010-11-29 08:14:05 +01002488fail_wmi_handler:
Corentin Charye12e6d92011-02-26 10:20:31 +01002489 asus_wmi_backlight_exit(asus);
Corentin Chary27c136c2010-11-29 08:14:05 +01002490fail_backlight:
Corentin Charye12e6d92011-02-26 10:20:31 +01002491 asus_wmi_rfkill_exit(asus);
Corentin Charyba48fdb2010-11-29 08:14:07 +01002492fail_rfkill:
Corentin Charye12e6d92011-02-26 10:20:31 +01002493 asus_wmi_led_exit(asus);
Corentin Chary084fca62010-11-29 08:14:06 +01002494fail_leds:
Corentin Charye07babd2011-02-26 10:20:42 +01002495fail_hwmon:
Corentin Charye12e6d92011-02-26 10:20:31 +01002496 asus_wmi_input_exit(asus);
Corentin Chary27c136c2010-11-29 08:14:05 +01002497fail_input:
Yurii Pavlovskyib096f622019-05-14 21:07:05 +02002498 asus_wmi_sysfs_exit(asus->platform_device);
2499fail_sysfs:
Daniel Drake9af93db2019-07-17 13:10:58 +08002500fail_fan_boost_mode:
Corentin Chary27c136c2010-11-29 08:14:05 +01002501fail_platform:
Corentin Charye12e6d92011-02-26 10:20:31 +01002502 kfree(asus);
Corentin Charya04ce292011-02-06 13:28:33 +01002503 return err;
Yong Wang45f2c692010-04-11 09:27:19 +08002504}
2505
Corentin Charye12e6d92011-02-26 10:20:31 +01002506static int asus_wmi_remove(struct platform_device *device)
Yong Wang45f2c692010-04-11 09:27:19 +08002507{
Corentin Charye12e6d92011-02-26 10:20:31 +01002508 struct asus_wmi *asus;
Yong Wang45f2c692010-04-11 09:27:19 +08002509
Corentin Charye12e6d92011-02-26 10:20:31 +01002510 asus = platform_get_drvdata(device);
2511 wmi_remove_notify_handler(asus->driver->event_guid);
2512 asus_wmi_backlight_exit(asus);
2513 asus_wmi_input_exit(asus);
2514 asus_wmi_led_exit(asus);
2515 asus_wmi_rfkill_exit(asus);
2516 asus_wmi_debugfs_exit(asus);
Yurii Pavlovskyib096f622019-05-14 21:07:05 +02002517 asus_wmi_sysfs_exit(asus->platform_device);
Daniel Drake2889ffc2019-07-29 16:27:37 +08002518 asus_fan_set_auto(asus);
Yong Wang45f2c692010-04-11 09:27:19 +08002519
Corentin Charye12e6d92011-02-26 10:20:31 +01002520 kfree(asus);
Yong Wang45f2c692010-04-11 09:27:19 +08002521 return 0;
2522}
2523
Yurii Pavlovskyi54a31212019-05-14 21:04:48 +02002524/* Platform driver - hibernate/resume callbacks *******************************/
2525
Corentin Charye12e6d92011-02-26 10:20:31 +01002526static int asus_hotk_thaw(struct device *device)
Corentin Chary0773d7f2011-02-06 13:28:32 +01002527{
Corentin Charye12e6d92011-02-26 10:20:31 +01002528 struct asus_wmi *asus = dev_get_drvdata(device);
Corentin Chary0773d7f2011-02-06 13:28:32 +01002529
Corentin Charya7ce3f02011-02-26 10:20:33 +01002530 if (asus->wlan.rfkill) {
Corentin Chary0773d7f2011-02-06 13:28:32 +01002531 bool wlan;
2532
2533 /*
2534 * Work around bios bug - acpi _PTS turns off the wireless led
2535 * during suspend. Normally it restores it on resume, but
2536 * we should kick it ourselves in case hibernation is aborted.
2537 */
Corentin Chary1d070f82011-02-26 10:20:36 +01002538 wlan = asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_WLAN);
Corentin Charye12e6d92011-02-26 10:20:31 +01002539 asus_wmi_set_devstate(ASUS_WMI_DEVID_WLAN, wlan, NULL);
Corentin Chary0773d7f2011-02-06 13:28:32 +01002540 }
2541
2542 return 0;
2543}
2544
Oleksij Rempel30734042015-09-14 11:16:30 +02002545static int asus_hotk_resume(struct device *device)
2546{
2547 struct asus_wmi *asus = dev_get_drvdata(device);
2548
2549 if (!IS_ERR_OR_NULL(asus->kbd_led.dev))
Jian-Hong Pan9fe44fc2018-09-27 16:50:09 +08002550 kbd_led_update(asus);
Oleksij Rempel30734042015-09-14 11:16:30 +02002551
Chris Chiu487579b2019-04-18 14:46:48 +08002552 if (asus_wmi_has_fnlock_key(asus))
2553 asus_wmi_fnlock_update(asus);
Oleksij Rempel30734042015-09-14 11:16:30 +02002554 return 0;
2555}
2556
Corentin Charye12e6d92011-02-26 10:20:31 +01002557static int asus_hotk_restore(struct device *device)
Corentin Chary0773d7f2011-02-06 13:28:32 +01002558{
Corentin Charye12e6d92011-02-26 10:20:31 +01002559 struct asus_wmi *asus = dev_get_drvdata(device);
Corentin Chary0773d7f2011-02-06 13:28:32 +01002560 int bl;
2561
2562 /* Refresh both wlan rfkill state and pci hotplug */
Corentin Charya7ce3f02011-02-26 10:20:33 +01002563 if (asus->wlan.rfkill)
Corentin Charye12e6d92011-02-26 10:20:31 +01002564 asus_rfkill_hotplug(asus);
Corentin Chary0773d7f2011-02-06 13:28:32 +01002565
Corentin Charya7ce3f02011-02-26 10:20:33 +01002566 if (asus->bluetooth.rfkill) {
Corentin Chary1d070f82011-02-26 10:20:36 +01002567 bl = !asus_wmi_get_devstate_simple(asus,
2568 ASUS_WMI_DEVID_BLUETOOTH);
Corentin Charya7ce3f02011-02-26 10:20:33 +01002569 rfkill_set_sw_state(asus->bluetooth.rfkill, bl);
Corentin Chary2e9e1592011-02-06 13:28:37 +01002570 }
Corentin Charya7ce3f02011-02-26 10:20:33 +01002571 if (asus->wimax.rfkill) {
Corentin Chary1d070f82011-02-26 10:20:36 +01002572 bl = !asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_WIMAX);
Corentin Charya7ce3f02011-02-26 10:20:33 +01002573 rfkill_set_sw_state(asus->wimax.rfkill, bl);
Corentin Chary2e9e1592011-02-06 13:28:37 +01002574 }
Corentin Charya7ce3f02011-02-26 10:20:33 +01002575 if (asus->wwan3g.rfkill) {
Corentin Chary1d070f82011-02-26 10:20:36 +01002576 bl = !asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_WWAN3G);
Corentin Charya7ce3f02011-02-26 10:20:33 +01002577 rfkill_set_sw_state(asus->wwan3g.rfkill, bl);
Corentin Chary0773d7f2011-02-06 13:28:32 +01002578 }
Corentin Chary43be8bd2011-07-01 11:34:40 +02002579 if (asus->gps.rfkill) {
2580 bl = !asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_GPS);
2581 rfkill_set_sw_state(asus->gps.rfkill, bl);
2582 }
Corentin Charya912d322011-07-01 11:34:41 +02002583 if (asus->uwb.rfkill) {
2584 bl = !asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_UWB);
2585 rfkill_set_sw_state(asus->uwb.rfkill, bl);
2586 }
Oleksij Rempel30734042015-09-14 11:16:30 +02002587 if (!IS_ERR_OR_NULL(asus->kbd_led.dev))
Jian-Hong Pan9fe44fc2018-09-27 16:50:09 +08002588 kbd_led_update(asus);
Corentin Chary0773d7f2011-02-06 13:28:32 +01002589
Chris Chiu487579b2019-04-18 14:46:48 +08002590 if (asus_wmi_has_fnlock_key(asus))
2591 asus_wmi_fnlock_update(asus);
Corentin Chary0773d7f2011-02-06 13:28:32 +01002592 return 0;
2593}
2594
Corentin Charye12e6d92011-02-26 10:20:31 +01002595static const struct dev_pm_ops asus_pm_ops = {
2596 .thaw = asus_hotk_thaw,
2597 .restore = asus_hotk_restore,
Oleksij Rempel30734042015-09-14 11:16:30 +02002598 .resume = asus_hotk_resume,
Corentin Chary0773d7f2011-02-06 13:28:32 +01002599};
2600
Yurii Pavlovskyi54a31212019-05-14 21:04:48 +02002601/* Registration ***************************************************************/
2602
Corentin Charye12e6d92011-02-26 10:20:31 +01002603static int asus_wmi_probe(struct platform_device *pdev)
Corentin Charyd358cb52010-11-29 08:14:14 +01002604{
Corentin Charye12e6d92011-02-26 10:20:31 +01002605 struct platform_driver *pdrv = to_platform_driver(pdev->dev.driver);
2606 struct asus_wmi_driver *wdrv = to_asus_wmi_driver(pdrv);
2607 int ret;
Corentin Charyd358cb52010-11-29 08:14:14 +01002608
Corentin Charye12e6d92011-02-26 10:20:31 +01002609 if (!wmi_has_guid(ASUS_WMI_MGMT_GUID)) {
Hans de Goedec9946112019-01-21 14:24:36 +01002610 pr_warn("ASUS Management GUID not found\n");
Yong Wangee027e42010-03-21 10:26:34 +08002611 return -ENODEV;
2612 }
2613
Corentin Charye12e6d92011-02-26 10:20:31 +01002614 if (wdrv->event_guid && !wmi_has_guid(wdrv->event_guid)) {
Hans de Goedec9946112019-01-21 14:24:36 +01002615 pr_warn("ASUS Event GUID not found\n");
Corentin Charyd358cb52010-11-29 08:14:14 +01002616 return -ENODEV;
2617 }
2618
Corentin Charye12e6d92011-02-26 10:20:31 +01002619 if (wdrv->probe) {
2620 ret = wdrv->probe(pdev);
2621 if (ret)
2622 return ret;
2623 }
2624
2625 return asus_wmi_add(pdev);
Corentin Charya04ce292011-02-06 13:28:33 +01002626}
Yong Wangee027e42010-03-21 10:26:34 +08002627
Corentin Charye12e6d92011-02-26 10:20:31 +01002628static bool used;
Yong Wangee027e42010-03-21 10:26:34 +08002629
Corentin Chary8fe8c252011-07-01 11:34:32 +02002630int __init_or_module asus_wmi_register_driver(struct asus_wmi_driver *driver)
Corentin Charya04ce292011-02-06 13:28:33 +01002631{
Corentin Charye12e6d92011-02-26 10:20:31 +01002632 struct platform_driver *platform_driver;
2633 struct platform_device *platform_device;
2634
2635 if (used)
2636 return -EBUSY;
2637
2638 platform_driver = &driver->platform_driver;
2639 platform_driver->remove = asus_wmi_remove;
2640 platform_driver->driver.owner = driver->owner;
2641 platform_driver->driver.name = driver->name;
2642 platform_driver->driver.pm = &asus_pm_ops;
2643
2644 platform_device = platform_create_bundle(platform_driver,
2645 asus_wmi_probe,
Corentin Charya04ce292011-02-06 13:28:33 +01002646 NULL, 0, NULL, 0);
2647 if (IS_ERR(platform_device))
2648 return PTR_ERR(platform_device);
Corentin Charye12e6d92011-02-26 10:20:31 +01002649
2650 used = true;
2651 return 0;
2652}
2653EXPORT_SYMBOL_GPL(asus_wmi_register_driver);
2654
2655void asus_wmi_unregister_driver(struct asus_wmi_driver *driver)
2656{
2657 platform_device_unregister(driver->platform_device);
2658 platform_driver_unregister(&driver->platform_driver);
2659 used = false;
2660}
2661EXPORT_SYMBOL_GPL(asus_wmi_unregister_driver);
2662
2663static int __init asus_wmi_init(void)
2664{
vic0ed60652013-05-22 21:32:10 +03002665 pr_info("ASUS WMI generic driver loaded\n");
Yong Wangee027e42010-03-21 10:26:34 +08002666 return 0;
2667}
2668
Corentin Charye12e6d92011-02-26 10:20:31 +01002669static void __exit asus_wmi_exit(void)
Yong Wangee027e42010-03-21 10:26:34 +08002670{
vic0ed60652013-05-22 21:32:10 +03002671 pr_info("ASUS WMI generic driver unloaded\n");
Yong Wangee027e42010-03-21 10:26:34 +08002672}
2673
Corentin Charye12e6d92011-02-26 10:20:31 +01002674module_init(asus_wmi_init);
2675module_exit(asus_wmi_exit);