blob: ca28d27dae63dd13d21f0e5a3886d166598d699c [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
68#define ASUS_FAN_CTRL_MANUAL 1
69#define ASUS_FAN_CTRL_AUTO 2
70
Daniel Drake9af93db2019-07-17 13:10:58 +080071#define ASUS_FAN_BOOST_MODE_NORMAL 0
72#define ASUS_FAN_BOOST_MODE_OVERBOOST 1
73#define ASUS_FAN_BOOST_MODE_OVERBOOST_MASK 0x01
74#define ASUS_FAN_BOOST_MODE_SILENT 2
75#define ASUS_FAN_BOOST_MODE_SILENT_MASK 0x02
76#define ASUS_FAN_BOOST_MODES_MASK 0x03
Yurii Pavlovskyib096f622019-05-14 21:07:05 +020077
Kai-Chuan Hsieh8023eff2016-09-01 23:55:55 +080078#define USB_INTEL_XUSB2PR 0xD0
79#define PCI_DEVICE_ID_INTEL_LYNXPOINT_LP_XHCI 0x9c31
80
Yurii Pavlovskyie0668f22019-05-14 21:00:31 +020081#define ASUS_ACPI_UID_ASUSWMI "ASUSWMI"
Yurii Pavlovskyi1a373d12019-05-14 21:02:09 +020082#define ASUS_ACPI_UID_ATK "ATK"
83
84#define WMI_EVENT_QUEUE_SIZE 0x10
85#define WMI_EVENT_QUEUE_END 0x1
86#define WMI_EVENT_MASK 0xFFFF
87/* The WMI hotkey event value is always the same. */
88#define WMI_EVENT_VALUE_ATK 0xFF
Yurii Pavlovskyie0668f22019-05-14 21:00:31 +020089
Yurii Pavlovskyi8abd752b2019-05-14 21:01:24 +020090#define WMI_EVENT_MASK 0xFFFF
91
João Paulo Rechi Vita71050ae2017-02-20 14:50:22 -050092static const char * const ashs_ids[] = { "ATK4001", "ATK4002", NULL };
93
João Paulo Rechi Vitacf48bf92018-05-22 14:30:15 -070094static bool ashs_present(void)
95{
96 int i = 0;
97 while (ashs_ids[i]) {
98 if (acpi_dev_found(ashs_ids[i++]))
99 return true;
100 }
101 return false;
102}
103
Yong Wang3d7b1652010-04-11 09:27:54 +0800104struct bios_args {
Corentin Charyd33da3b2011-02-26 10:20:35 +0100105 u32 arg0;
106 u32 arg1;
Yurii Pavlovskyi98e865a2019-05-14 20:54:50 +0200107 u32 arg2; /* At least TUF Gaming series uses 3 dword input buffer. */
Corentin Charyd33da3b2011-02-26 10:20:35 +0100108} __packed;
Yong Wang3d7b1652010-04-11 09:27:54 +0800109
Corentin Chary8c1b2d82010-11-29 08:14:09 +0100110/*
Kast Bernd53e755c2015-05-13 16:24:16 +0200111 * Struct that's used for all methods called via AGFN. Naming is
112 * identically to the AML code.
113 */
114struct agfn_args {
115 u16 mfun; /* probably "Multi-function" to be called */
116 u16 sfun; /* probably "Sub-function" to be called */
117 u16 len; /* size of the hole struct, including subfunction fields */
118 u8 stas; /* not used by now */
119 u8 err; /* zero on success */
120} __packed;
121
122/* struct used for calling fan read and write methods */
123struct fan_args {
124 struct agfn_args agfn; /* common fields */
125 u8 fan; /* fan number: 0: set auto mode 1: 1st fan */
126 u32 speed; /* read: RPM/100 - write: 0-255 */
127} __packed;
128
129/*
Corentin Charye12e6d92011-02-26 10:20:31 +0100130 * <platform>/ - debugfs root directory
Corentin Chary8c1b2d82010-11-29 08:14:09 +0100131 * dev_id - current dev_id
132 * ctrl_param - current ctrl_param
Corentin Charyef343492011-02-26 10:20:39 +0100133 * method_id - current method_id
Corentin Chary8c1b2d82010-11-29 08:14:09 +0100134 * devs - call DEVS(dev_id, ctrl_param) and print result
135 * dsts - call DSTS(dev_id) and print result
Corentin Charyef343492011-02-26 10:20:39 +0100136 * call - call method_id(dev_id, ctrl_param) and print result
Corentin Chary8c1b2d82010-11-29 08:14:09 +0100137 */
Corentin Charye12e6d92011-02-26 10:20:31 +0100138struct asus_wmi_debug {
Corentin Chary8c1b2d82010-11-29 08:14:09 +0100139 struct dentry *root;
Corentin Charyef343492011-02-26 10:20:39 +0100140 u32 method_id;
Corentin Chary8c1b2d82010-11-29 08:14:09 +0100141 u32 dev_id;
142 u32 ctrl_param;
143};
144
Corentin Charya7ce3f02011-02-26 10:20:33 +0100145struct asus_rfkill {
146 struct asus_wmi *asus;
147 struct rfkill *rfkill;
148 u32 dev_id;
149};
150
Corentin Charye12e6d92011-02-26 10:20:31 +0100151struct asus_wmi {
Corentin Chary1d070f82011-02-26 10:20:36 +0100152 int dsts_id;
Corentin Chary46dbca82011-02-26 10:20:38 +0100153 int spec;
154 int sfun;
Yurii Pavlovskyi1a373d12019-05-14 21:02:09 +0200155 bool wmi_event_queue;
Corentin Chary1d070f82011-02-26 10:20:36 +0100156
Yong Wang81248882010-04-11 09:26:33 +0800157 struct input_dev *inputdev;
Yong Wang3d7b1652010-04-11 09:27:54 +0800158 struct backlight_device *backlight_device;
Corentin Chary27c136c2010-11-29 08:14:05 +0100159 struct platform_device *platform_device;
Corentin Chary084fca62010-11-29 08:14:06 +0100160
AceLan Kao6cae06e2012-07-27 16:51:59 +0800161 struct led_classdev wlan_led;
162 int wlan_led_wk;
Corentin Chary084fca62010-11-29 08:14:06 +0100163 struct led_classdev tpd_led;
164 int tpd_led_wk;
Corentin Charye9809c02011-07-01 11:34:31 +0200165 struct led_classdev kbd_led;
166 int kbd_led_wk;
Maxime Bellengé4c059842017-09-23 18:23:37 +0200167 struct led_classdev lightbar_led;
168 int lightbar_led_wk;
Corentin Chary084fca62010-11-29 08:14:06 +0100169 struct workqueue_struct *led_workqueue;
170 struct work_struct tpd_led_work;
AceLan Kao6cae06e2012-07-27 16:51:59 +0800171 struct work_struct wlan_led_work;
Maxime Bellengé4c059842017-09-23 18:23:37 +0200172 struct work_struct lightbar_led_work;
Corentin Charyba48fdb2010-11-29 08:14:07 +0100173
Corentin Charya7ce3f02011-02-26 10:20:33 +0100174 struct asus_rfkill wlan;
175 struct asus_rfkill bluetooth;
176 struct asus_rfkill wimax;
177 struct asus_rfkill wwan3g;
Corentin Chary43be8bd2011-07-01 11:34:40 +0200178 struct asus_rfkill gps;
Corentin Charya912d322011-07-01 11:34:41 +0200179 struct asus_rfkill uwb;
Corentin Chary8c1b2d82010-11-29 08:14:09 +0100180
Kast Bernd53e755c2015-05-13 16:24:16 +0200181 bool asus_hwmon_fan_manual_mode;
182 int asus_hwmon_num_fans;
183 int asus_hwmon_pwm;
184
Daniel Drake9af93db2019-07-17 13:10:58 +0800185 bool fan_boost_mode_available;
186 u8 fan_boost_mode_mask;
187 u8 fan_boost_mode;
Yurii Pavlovskyib096f622019-05-14 21:07:05 +0200188
Lukas Wunner125450f2018-09-08 09:59:01 +0200189 struct hotplug_slot hotplug_slot;
Corentin Charyafa7c882011-02-06 13:28:28 +0100190 struct mutex hotplug_lock;
Corentin Chary279f8f92011-02-06 13:28:29 +0100191 struct mutex wmi_lock;
192 struct workqueue_struct *hotplug_workqueue;
193 struct work_struct hotplug_work;
Corentin Charyafa7c882011-02-06 13:28:28 +0100194
Chris Chiu487579b2019-04-18 14:46:48 +0800195 bool fnlock_locked;
196
Corentin Charye12e6d92011-02-26 10:20:31 +0100197 struct asus_wmi_debug debug;
198
199 struct asus_wmi_driver *driver;
Yong Wang81248882010-04-11 09:26:33 +0800200};
201
Yurii Pavlovskyi54a31212019-05-14 21:04:48 +0200202/* Input **********************************************************************/
203
Corentin Charye12e6d92011-02-26 10:20:31 +0100204static int asus_wmi_input_init(struct asus_wmi *asus)
Yong Wangee027e42010-03-21 10:26:34 +0800205{
206 int err;
207
Corentin Charye12e6d92011-02-26 10:20:31 +0100208 asus->inputdev = input_allocate_device();
209 if (!asus->inputdev)
Yong Wangee027e42010-03-21 10:26:34 +0800210 return -ENOMEM;
211
Corentin Chary58a9f392011-03-30 16:32:32 +0200212 asus->inputdev->name = asus->driver->input_name;
213 asus->inputdev->phys = asus->driver->input_phys;
Corentin Charye12e6d92011-02-26 10:20:31 +0100214 asus->inputdev->id.bustype = BUS_HOST;
215 asus->inputdev->dev.parent = &asus->platform_device->dev;
Seth Forshee39bbde02011-07-04 09:49:20 +0200216 set_bit(EV_REP, asus->inputdev->evbit);
Yong Wangee027e42010-03-21 10:26:34 +0800217
Corentin Charye12e6d92011-02-26 10:20:31 +0100218 err = sparse_keymap_setup(asus->inputdev, asus->driver->keymap, NULL);
Yong Wangee027e42010-03-21 10:26:34 +0800219 if (err)
220 goto err_free_dev;
221
Corentin Charye12e6d92011-02-26 10:20:31 +0100222 err = input_register_device(asus->inputdev);
Yong Wangee027e42010-03-21 10:26:34 +0800223 if (err)
Michał Kępieńc2ef3a12017-03-09 13:11:39 +0100224 goto err_free_dev;
Yong Wangee027e42010-03-21 10:26:34 +0800225
226 return 0;
227
Yong Wangee027e42010-03-21 10:26:34 +0800228err_free_dev:
Corentin Charye12e6d92011-02-26 10:20:31 +0100229 input_free_device(asus->inputdev);
Yong Wangee027e42010-03-21 10:26:34 +0800230 return err;
231}
232
Corentin Charye12e6d92011-02-26 10:20:31 +0100233static void asus_wmi_input_exit(struct asus_wmi *asus)
Yong Wang81248882010-04-11 09:26:33 +0800234{
Michał Kępieńc2ef3a12017-03-09 13:11:39 +0100235 if (asus->inputdev)
Corentin Charye12e6d92011-02-26 10:20:31 +0100236 input_unregister_device(asus->inputdev);
Yong Wang81248882010-04-11 09:26:33 +0800237
Corentin Charye12e6d92011-02-26 10:20:31 +0100238 asus->inputdev = NULL;
Yong Wang81248882010-04-11 09:26:33 +0800239}
240
Yurii Pavlovskyi54a31212019-05-14 21:04:48 +0200241/* WMI ************************************************************************/
242
Yurii Pavlovskyi98e865a2019-05-14 20:54:50 +0200243static int asus_wmi_evaluate_method3(u32 method_id,
244 u32 arg0, u32 arg1, u32 arg2, u32 *retval)
Yong Wang3d7b1652010-04-11 09:27:54 +0800245{
Corentin Charyd33da3b2011-02-26 10:20:35 +0100246 struct bios_args args = {
247 .arg0 = arg0,
248 .arg1 = arg1,
Yurii Pavlovskyi98e865a2019-05-14 20:54:50 +0200249 .arg2 = arg2,
Corentin Charyd33da3b2011-02-26 10:20:35 +0100250 };
251 struct acpi_buffer input = { (acpi_size) sizeof(args), &args };
Yong Wang3d7b1652010-04-11 09:27:54 +0800252 struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER, NULL };
Yong Wang3d7b1652010-04-11 09:27:54 +0800253 acpi_status status;
Corentin Charyd33da3b2011-02-26 10:20:35 +0100254 union acpi_object *obj;
Rickard Strandqvist8ad3be12014-06-01 14:58:52 +0200255 u32 tmp = 0;
Yong Wang3d7b1652010-04-11 09:27:54 +0800256
Pali Rohár0fe57262017-08-12 09:44:16 +0200257 status = wmi_evaluate_method(ASUS_WMI_MGMT_GUID, 0, method_id,
Corentin Charyafa7c882011-02-06 13:28:28 +0100258 &input, &output);
Yong Wang3d7b1652010-04-11 09:27:54 +0800259
260 if (ACPI_FAILURE(status))
Yurii Pavlovskyi1827f3f2019-05-14 21:03:50 +0200261 return -EIO;
Yong Wang3d7b1652010-04-11 09:27:54 +0800262
263 obj = (union acpi_object *)output.pointer;
264 if (obj && obj->type == ACPI_TYPE_INTEGER)
Corentin Charye12e6d92011-02-26 10:20:31 +0100265 tmp = (u32) obj->integer.value;
Yong Wang3d7b1652010-04-11 09:27:54 +0800266
Corentin Chary2a3f0062010-11-29 08:14:10 +0100267 if (retval)
268 *retval = tmp;
Yong Wang3d7b1652010-04-11 09:27:54 +0800269
270 kfree(obj);
271
Corentin Charyd33da3b2011-02-26 10:20:35 +0100272 if (tmp == ASUS_WMI_UNSUPPORTED_METHOD)
273 return -ENODEV;
274
275 return 0;
Yong Wang3d7b1652010-04-11 09:27:54 +0800276}
Yurii Pavlovskyi98e865a2019-05-14 20:54:50 +0200277
278int asus_wmi_evaluate_method(u32 method_id, u32 arg0, u32 arg1, u32 *retval)
279{
280 return asus_wmi_evaluate_method3(method_id, arg0, arg1, 0, retval);
281}
Daniel Drakeffb6ce72018-10-09 14:40:55 +0800282EXPORT_SYMBOL_GPL(asus_wmi_evaluate_method);
Yong Wang3d7b1652010-04-11 09:27:54 +0800283
Kast Bernd53e755c2015-05-13 16:24:16 +0200284static int asus_wmi_evaluate_method_agfn(const struct acpi_buffer args)
285{
286 struct acpi_buffer input;
287 u64 phys_addr;
288 u32 retval;
289 u32 status = -1;
290
291 /*
292 * Copy to dma capable address otherwise memory corruption occurs as
293 * bios has to be able to access it.
294 */
295 input.pointer = kzalloc(args.length, GFP_DMA | GFP_KERNEL);
296 input.length = args.length;
297 if (!input.pointer)
298 return -ENOMEM;
299 phys_addr = virt_to_phys(input.pointer);
300 memcpy(input.pointer, args.pointer, args.length);
301
302 status = asus_wmi_evaluate_method(ASUS_WMI_METHODID_AGFN,
303 phys_addr, 0, &retval);
304 if (!status)
305 memcpy(args.pointer, input.pointer, args.length);
306
307 kfree(input.pointer);
308 if (status)
309 return -ENXIO;
310
311 return retval;
312}
313
Corentin Chary1d070f82011-02-26 10:20:36 +0100314static int asus_wmi_get_devstate(struct asus_wmi *asus, u32 dev_id, u32 *retval)
Corentin Charyd33da3b2011-02-26 10:20:35 +0100315{
Corentin Chary1d070f82011-02-26 10:20:36 +0100316 return asus_wmi_evaluate_method(asus->dsts_id, dev_id, 0, retval);
Corentin Charyd33da3b2011-02-26 10:20:35 +0100317}
318
319static int asus_wmi_set_devstate(u32 dev_id, u32 ctrl_param,
Corentin Chary1d070f82011-02-26 10:20:36 +0100320 u32 *retval)
Yong Wang3d7b1652010-04-11 09:27:54 +0800321{
Corentin Charyd33da3b2011-02-26 10:20:35 +0100322 return asus_wmi_evaluate_method(ASUS_WMI_METHODID_DEVS, dev_id,
323 ctrl_param, retval);
Yong Wang3d7b1652010-04-11 09:27:54 +0800324}
325
Corentin Chary5c956382011-02-06 13:28:31 +0100326/* Helper for special devices with magic return codes */
Corentin Chary1d070f82011-02-26 10:20:36 +0100327static int asus_wmi_get_devstate_bits(struct asus_wmi *asus,
328 u32 dev_id, u32 mask)
Corentin Chary5c956382011-02-06 13:28:31 +0100329{
330 u32 retval = 0;
Corentin Charyd33da3b2011-02-26 10:20:35 +0100331 int err;
Corentin Chary5c956382011-02-06 13:28:31 +0100332
Corentin Chary1d070f82011-02-26 10:20:36 +0100333 err = asus_wmi_get_devstate(asus, dev_id, &retval);
Corentin Chary5c956382011-02-06 13:28:31 +0100334
Corentin Charyd33da3b2011-02-26 10:20:35 +0100335 if (err < 0)
336 return err;
Corentin Chary5c956382011-02-06 13:28:31 +0100337
Corentin Charye12e6d92011-02-26 10:20:31 +0100338 if (!(retval & ASUS_WMI_DSTS_PRESENCE_BIT))
Corentin Chary5c956382011-02-06 13:28:31 +0100339 return -ENODEV;
340
Corentin Charya75fe0d2011-02-26 10:20:34 +0100341 if (mask == ASUS_WMI_DSTS_STATUS_BIT) {
342 if (retval & ASUS_WMI_DSTS_UNKNOWN_BIT)
343 return -ENODEV;
344 }
345
Corentin Charyb7187262011-02-06 13:28:39 +0100346 return retval & mask;
347}
348
Corentin Chary1d070f82011-02-26 10:20:36 +0100349static int asus_wmi_get_devstate_simple(struct asus_wmi *asus, u32 dev_id)
Corentin Charyb7187262011-02-06 13:28:39 +0100350{
Corentin Chary1d070f82011-02-26 10:20:36 +0100351 return asus_wmi_get_devstate_bits(asus, dev_id,
352 ASUS_WMI_DSTS_STATUS_BIT);
Corentin Chary5c956382011-02-06 13:28:31 +0100353}
354
Yurii Pavlovskyi54a31212019-05-14 21:04:48 +0200355/* LEDs ***********************************************************************/
356
Corentin Chary084fca62010-11-29 08:14:06 +0100357/*
358 * These functions actually update the LED's, and are called from a
359 * workqueue. By doing this as separate work rather than when the LED
Corentin Charye12e6d92011-02-26 10:20:31 +0100360 * subsystem asks, we avoid messing with the Asus ACPI stuff during a
Corentin Chary084fca62010-11-29 08:14:06 +0100361 * potentially bad time, such as a timer interrupt.
362 */
363static void tpd_led_update(struct work_struct *work)
364{
365 int ctrl_param;
Corentin Charye12e6d92011-02-26 10:20:31 +0100366 struct asus_wmi *asus;
Corentin Chary084fca62010-11-29 08:14:06 +0100367
Corentin Charye12e6d92011-02-26 10:20:31 +0100368 asus = container_of(work, struct asus_wmi, tpd_led_work);
Corentin Chary084fca62010-11-29 08:14:06 +0100369
Corentin Charye12e6d92011-02-26 10:20:31 +0100370 ctrl_param = asus->tpd_led_wk;
371 asus_wmi_set_devstate(ASUS_WMI_DEVID_TOUCHPAD_LED, ctrl_param, NULL);
Corentin Chary084fca62010-11-29 08:14:06 +0100372}
373
374static void tpd_led_set(struct led_classdev *led_cdev,
375 enum led_brightness value)
376{
Corentin Charye12e6d92011-02-26 10:20:31 +0100377 struct asus_wmi *asus;
Corentin Chary084fca62010-11-29 08:14:06 +0100378
Corentin Charye12e6d92011-02-26 10:20:31 +0100379 asus = container_of(led_cdev, struct asus_wmi, tpd_led);
Corentin Chary084fca62010-11-29 08:14:06 +0100380
Corentin Charye12e6d92011-02-26 10:20:31 +0100381 asus->tpd_led_wk = !!value;
382 queue_work(asus->led_workqueue, &asus->tpd_led_work);
Corentin Chary084fca62010-11-29 08:14:06 +0100383}
384
Corentin Charye12e6d92011-02-26 10:20:31 +0100385static int read_tpd_led_state(struct asus_wmi *asus)
Corentin Chary084fca62010-11-29 08:14:06 +0100386{
Corentin Chary1d070f82011-02-26 10:20:36 +0100387 return asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_TOUCHPAD_LED);
Corentin Chary084fca62010-11-29 08:14:06 +0100388}
389
390static enum led_brightness tpd_led_get(struct led_classdev *led_cdev)
391{
Corentin Charye12e6d92011-02-26 10:20:31 +0100392 struct asus_wmi *asus;
Corentin Chary084fca62010-11-29 08:14:06 +0100393
Corentin Charye12e6d92011-02-26 10:20:31 +0100394 asus = container_of(led_cdev, struct asus_wmi, tpd_led);
Corentin Chary084fca62010-11-29 08:14:06 +0100395
Corentin Charye12e6d92011-02-26 10:20:31 +0100396 return read_tpd_led_state(asus);
Corentin Chary084fca62010-11-29 08:14:06 +0100397}
398
Jian-Hong Pan9fe44fc2018-09-27 16:50:09 +0800399static void kbd_led_update(struct asus_wmi *asus)
Corentin Chary084fca62010-11-29 08:14:06 +0100400{
Corentin Charye9809c02011-07-01 11:34:31 +0200401 int ctrl_param = 0;
Corentin Chary084fca62010-11-29 08:14:06 +0100402
Corentin Charye9809c02011-07-01 11:34:31 +0200403 /*
404 * bits 0-2: level
405 * bit 7: light on/off
406 */
407 if (asus->kbd_led_wk > 0)
408 ctrl_param = 0x80 | (asus->kbd_led_wk & 0x7F);
Corentin Chary084fca62010-11-29 08:14:06 +0100409
Corentin Charye9809c02011-07-01 11:34:31 +0200410 asus_wmi_set_devstate(ASUS_WMI_DEVID_KBD_BACKLIGHT, ctrl_param, NULL);
411}
Corentin Chary084fca62010-11-29 08:14:06 +0100412
Corentin Charye9809c02011-07-01 11:34:31 +0200413static int kbd_led_read(struct asus_wmi *asus, int *level, int *env)
414{
415 int retval;
416
417 /*
418 * bits 0-2: level
419 * bit 7: light on/off
420 * bit 8-10: environment (0: dark, 1: normal, 2: light)
421 * bit 17: status unknown
422 */
423 retval = asus_wmi_get_devstate_bits(asus, ASUS_WMI_DEVID_KBD_BACKLIGHT,
424 0xFFFF);
425
Corentin Charyaf965e92011-07-01 11:34:34 +0200426 /* Unknown status is considered as off */
Corentin Charye9809c02011-07-01 11:34:31 +0200427 if (retval == 0x8000)
Corentin Charyaf965e92011-07-01 11:34:34 +0200428 retval = 0;
Corentin Charye9809c02011-07-01 11:34:31 +0200429
430 if (retval >= 0) {
431 if (level)
Corentin Charyc09b2232012-03-20 09:53:04 +0100432 *level = retval & 0x7F;
Corentin Charye9809c02011-07-01 11:34:31 +0200433 if (env)
434 *env = (retval >> 8) & 0x7F;
435 retval = 0;
Corentin Chary084fca62010-11-29 08:14:06 +0100436 }
437
Corentin Charye9809c02011-07-01 11:34:31 +0200438 return retval;
439}
440
Chris Chiudbb3d782018-06-20 22:46:44 +0800441static void do_kbd_led_set(struct led_classdev *led_cdev, int value)
Corentin Charye9809c02011-07-01 11:34:31 +0200442{
443 struct asus_wmi *asus;
Chris Chiudbb3d782018-06-20 22:46:44 +0800444 int max_level;
Corentin Charye9809c02011-07-01 11:34:31 +0200445
446 asus = container_of(led_cdev, struct asus_wmi, kbd_led);
Chris Chiudbb3d782018-06-20 22:46:44 +0800447 max_level = asus->kbd_led.max_brightness;
Corentin Charye9809c02011-07-01 11:34:31 +0200448
Chris Chiudbb3d782018-06-20 22:46:44 +0800449 if (value > max_level)
450 value = max_level;
Corentin Charye9809c02011-07-01 11:34:31 +0200451 else if (value < 0)
452 value = 0;
453
454 asus->kbd_led_wk = value;
Jian-Hong Pan9fe44fc2018-09-27 16:50:09 +0800455 kbd_led_update(asus);
Corentin Charye9809c02011-07-01 11:34:31 +0200456}
457
Chris Chiudbb3d782018-06-20 22:46:44 +0800458static void kbd_led_set(struct led_classdev *led_cdev,
459 enum led_brightness value)
460{
Yurii Pavlovskyi3e581672019-05-14 21:07:46 +0200461 /* Prevent disabling keyboard backlight on module unregister */
462 if (led_cdev->flags & LED_UNREGISTERING)
463 return;
464
Chris Chiudbb3d782018-06-20 22:46:44 +0800465 do_kbd_led_set(led_cdev, value);
466}
467
Jian-Hong Pan29f6eb52018-10-22 18:00:04 +0800468static void kbd_led_set_by_kbd(struct asus_wmi *asus, enum led_brightness value)
469{
470 struct led_classdev *led_cdev = &asus->kbd_led;
471
472 do_kbd_led_set(led_cdev, value);
473 led_classdev_notify_brightness_hw_changed(led_cdev, asus->kbd_led_wk);
474}
475
Corentin Charye9809c02011-07-01 11:34:31 +0200476static enum led_brightness kbd_led_get(struct led_classdev *led_cdev)
477{
478 struct asus_wmi *asus;
479 int retval, value;
480
481 asus = container_of(led_cdev, struct asus_wmi, kbd_led);
482
483 retval = kbd_led_read(asus, &value, NULL);
484
485 if (retval < 0)
486 return retval;
487
488 return value;
Corentin Chary084fca62010-11-29 08:14:06 +0100489}
490
AceLan Kao6cae06e2012-07-27 16:51:59 +0800491static int wlan_led_unknown_state(struct asus_wmi *asus)
492{
493 u32 result;
494
495 asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_WIRELESS_LED, &result);
496
497 return result & ASUS_WMI_DSTS_UNKNOWN_BIT;
498}
499
500static int wlan_led_presence(struct asus_wmi *asus)
501{
502 u32 result;
503
504 asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_WIRELESS_LED, &result);
505
506 return result & ASUS_WMI_DSTS_PRESENCE_BIT;
507}
508
509static void wlan_led_update(struct work_struct *work)
510{
511 int ctrl_param;
512 struct asus_wmi *asus;
513
514 asus = container_of(work, struct asus_wmi, wlan_led_work);
515
516 ctrl_param = asus->wlan_led_wk;
517 asus_wmi_set_devstate(ASUS_WMI_DEVID_WIRELESS_LED, ctrl_param, NULL);
518}
519
520static void wlan_led_set(struct led_classdev *led_cdev,
521 enum led_brightness value)
522{
523 struct asus_wmi *asus;
524
525 asus = container_of(led_cdev, struct asus_wmi, wlan_led);
526
527 asus->wlan_led_wk = !!value;
528 queue_work(asus->led_workqueue, &asus->wlan_led_work);
529}
530
531static enum led_brightness wlan_led_get(struct led_classdev *led_cdev)
532{
533 struct asus_wmi *asus;
534 u32 result;
535
536 asus = container_of(led_cdev, struct asus_wmi, wlan_led);
537 asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_WIRELESS_LED, &result);
538
539 return result & ASUS_WMI_DSTS_BRIGHTNESS_MASK;
540}
541
Maxime Bellengé4c059842017-09-23 18:23:37 +0200542static void lightbar_led_update(struct work_struct *work)
543{
544 struct asus_wmi *asus;
545 int ctrl_param;
546
547 asus = container_of(work, struct asus_wmi, lightbar_led_work);
548
549 ctrl_param = asus->lightbar_led_wk;
550 asus_wmi_set_devstate(ASUS_WMI_DEVID_LIGHTBAR, ctrl_param, NULL);
551}
552
553static void lightbar_led_set(struct led_classdev *led_cdev,
554 enum led_brightness value)
555{
556 struct asus_wmi *asus;
557
558 asus = container_of(led_cdev, struct asus_wmi, lightbar_led);
559
560 asus->lightbar_led_wk = !!value;
561 queue_work(asus->led_workqueue, &asus->lightbar_led_work);
562}
563
564static enum led_brightness lightbar_led_get(struct led_classdev *led_cdev)
565{
566 struct asus_wmi *asus;
567 u32 result;
568
569 asus = container_of(led_cdev, struct asus_wmi, lightbar_led);
570 asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_LIGHTBAR, &result);
571
572 return result & ASUS_WMI_DSTS_LIGHTBAR_MASK;
573}
574
575static int lightbar_led_presence(struct asus_wmi *asus)
576{
577 u32 result;
578
579 asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_LIGHTBAR, &result);
580
581 return result & ASUS_WMI_DSTS_PRESENCE_BIT;
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
AceLan Kaof5156232014-07-09 16:18:18 +0800634 if (wlan_led_presence(asus) && (asus->driver->quirks->wapf > 0)) {
AceLan Kao6cae06e2012-07-27 16:51:59 +0800635 INIT_WORK(&asus->wlan_led_work, wlan_led_update);
636
637 asus->wlan_led.name = "asus::wlan";
638 asus->wlan_led.brightness_set = wlan_led_set;
639 if (!wlan_led_unknown_state(asus))
640 asus->wlan_led.brightness_get = wlan_led_get;
641 asus->wlan_led.flags = LED_CORE_SUSPENDRESUME;
642 asus->wlan_led.max_brightness = 1;
643 asus->wlan_led.default_trigger = "asus-wlan";
644
645 rv = led_classdev_register(&asus->platform_device->dev,
646 &asus->wlan_led);
Maxime Bellengé4c059842017-09-23 18:23:37 +0200647 if (rv)
648 goto error;
649 }
650
651 if (lightbar_led_presence(asus)) {
652 INIT_WORK(&asus->lightbar_led_work, lightbar_led_update);
653
654 asus->lightbar_led.name = "asus::lightbar";
655 asus->lightbar_led.brightness_set = lightbar_led_set;
656 asus->lightbar_led.brightness_get = lightbar_led_get;
657 asus->lightbar_led.max_brightness = 1;
658
659 rv = led_classdev_register(&asus->platform_device->dev,
660 &asus->lightbar_led);
Corentin Charye9809c02011-07-01 11:34:31 +0200661 }
662
663error:
664 if (rv)
665 asus_wmi_led_exit(asus);
666
667 return rv;
668}
669
Yurii Pavlovskyi54a31212019-05-14 21:04:48 +0200670/* RF *************************************************************************/
Corentin Charye9809c02011-07-01 11:34:31 +0200671
Corentin Chary084fca62010-11-29 08:14:06 +0100672/*
Corentin Charyafa7c882011-02-06 13:28:28 +0100673 * PCI hotplug (for wlan rfkill)
674 */
Corentin Charye12e6d92011-02-26 10:20:31 +0100675static bool asus_wlan_rfkill_blocked(struct asus_wmi *asus)
Corentin Charyafa7c882011-02-06 13:28:28 +0100676{
Corentin Chary1d070f82011-02-26 10:20:36 +0100677 int result = asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_WLAN);
Corentin Charyafa7c882011-02-06 13:28:28 +0100678
Corentin Chary5c956382011-02-06 13:28:31 +0100679 if (result < 0)
Corentin Charyafa7c882011-02-06 13:28:28 +0100680 return false;
Corentin Chary5c956382011-02-06 13:28:31 +0100681 return !result;
Corentin Charyafa7c882011-02-06 13:28:28 +0100682}
683
Corentin Charye12e6d92011-02-26 10:20:31 +0100684static void asus_rfkill_hotplug(struct asus_wmi *asus)
Corentin Charyafa7c882011-02-06 13:28:28 +0100685{
686 struct pci_dev *dev;
687 struct pci_bus *bus;
Corentin Chary279f8f92011-02-06 13:28:29 +0100688 bool blocked;
Corentin Charyafa7c882011-02-06 13:28:28 +0100689 bool absent;
690 u32 l;
691
Corentin Charye12e6d92011-02-26 10:20:31 +0100692 mutex_lock(&asus->wmi_lock);
693 blocked = asus_wlan_rfkill_blocked(asus);
694 mutex_unlock(&asus->wmi_lock);
Corentin Charyafa7c882011-02-06 13:28:28 +0100695
Corentin Charye12e6d92011-02-26 10:20:31 +0100696 mutex_lock(&asus->hotplug_lock);
Rafael J. Wysocki8b9ec1d2014-01-10 15:27:08 +0100697 pci_lock_rescan_remove();
Corentin Charyafa7c882011-02-06 13:28:28 +0100698
Corentin Charya7ce3f02011-02-26 10:20:33 +0100699 if (asus->wlan.rfkill)
700 rfkill_set_sw_state(asus->wlan.rfkill, blocked);
Corentin Chary279f8f92011-02-06 13:28:29 +0100701
Lukas Wunner125450f2018-09-08 09:59:01 +0200702 if (asus->hotplug_slot.ops) {
Corentin Charyafa7c882011-02-06 13:28:28 +0100703 bus = pci_find_bus(0, 1);
704 if (!bus) {
Joe Perches5ad77dc2011-03-29 15:21:35 -0700705 pr_warn("Unable to find PCI bus 1?\n");
Corentin Charyafa7c882011-02-06 13:28:28 +0100706 goto out_unlock;
707 }
708
709 if (pci_bus_read_config_dword(bus, 0, PCI_VENDOR_ID, &l)) {
710 pr_err("Unable to read PCI config space?\n");
711 goto out_unlock;
712 }
713 absent = (l == 0xffffffff);
714
715 if (blocked != absent) {
Joe Perches5ad77dc2011-03-29 15:21:35 -0700716 pr_warn("BIOS says wireless lan is %s, "
717 "but the pci device is %s\n",
718 blocked ? "blocked" : "unblocked",
719 absent ? "absent" : "present");
720 pr_warn("skipped wireless hotplug as probably "
721 "inappropriate for this model\n");
Corentin Charyafa7c882011-02-06 13:28:28 +0100722 goto out_unlock;
723 }
724
725 if (!blocked) {
726 dev = pci_get_slot(bus, 0);
727 if (dev) {
728 /* Device already present */
729 pci_dev_put(dev);
730 goto out_unlock;
731 }
732 dev = pci_scan_single_device(bus, 0);
733 if (dev) {
734 pci_bus_assign_resources(bus);
Yijing Wangc893d132014-05-30 11:01:03 +0800735 pci_bus_add_device(dev);
Corentin Charyafa7c882011-02-06 13:28:28 +0100736 }
737 } else {
738 dev = pci_get_slot(bus, 0);
739 if (dev) {
Yinghai Lu210647a2012-02-25 13:54:20 -0800740 pci_stop_and_remove_bus_device(dev);
Corentin Charyafa7c882011-02-06 13:28:28 +0100741 pci_dev_put(dev);
742 }
743 }
744 }
745
746out_unlock:
Rafael J. Wysocki8b9ec1d2014-01-10 15:27:08 +0100747 pci_unlock_rescan_remove();
Corentin Charye12e6d92011-02-26 10:20:31 +0100748 mutex_unlock(&asus->hotplug_lock);
Corentin Charyafa7c882011-02-06 13:28:28 +0100749}
750
Corentin Charye12e6d92011-02-26 10:20:31 +0100751static void asus_rfkill_notify(acpi_handle handle, u32 event, void *data)
Corentin Charyafa7c882011-02-06 13:28:28 +0100752{
Corentin Charye12e6d92011-02-26 10:20:31 +0100753 struct asus_wmi *asus = data;
Corentin Charyafa7c882011-02-06 13:28:28 +0100754
755 if (event != ACPI_NOTIFY_BUS_CHECK)
756 return;
757
Corentin Chary279f8f92011-02-06 13:28:29 +0100758 /*
Corentin Charye12e6d92011-02-26 10:20:31 +0100759 * We can't call directly asus_rfkill_hotplug because most
Corentin Chary279f8f92011-02-06 13:28:29 +0100760 * of the time WMBC is still being executed and not reetrant.
761 * There is currently no way to tell ACPICA that we want this
Corentin Charye12e6d92011-02-26 10:20:31 +0100762 * method to be serialized, we schedule a asus_rfkill_hotplug
Corentin Chary279f8f92011-02-06 13:28:29 +0100763 * call later, in a safer context.
764 */
Corentin Charye12e6d92011-02-26 10:20:31 +0100765 queue_work(asus->hotplug_workqueue, &asus->hotplug_work);
Corentin Charyafa7c882011-02-06 13:28:28 +0100766}
767
Corentin Charye12e6d92011-02-26 10:20:31 +0100768static int asus_register_rfkill_notifier(struct asus_wmi *asus, char *node)
Corentin Charyafa7c882011-02-06 13:28:28 +0100769{
770 acpi_status status;
771 acpi_handle handle;
772
773 status = acpi_get_handle(NULL, node, &handle);
774
775 if (ACPI_SUCCESS(status)) {
776 status = acpi_install_notify_handler(handle,
777 ACPI_SYSTEM_NOTIFY,
Corentin Charye12e6d92011-02-26 10:20:31 +0100778 asus_rfkill_notify, asus);
Corentin Charyafa7c882011-02-06 13:28:28 +0100779 if (ACPI_FAILURE(status))
Joe Perches5ad77dc2011-03-29 15:21:35 -0700780 pr_warn("Failed to register notify on %s\n", node);
Corentin Charyafa7c882011-02-06 13:28:28 +0100781 } else
782 return -ENODEV;
783
784 return 0;
785}
786
Corentin Charye12e6d92011-02-26 10:20:31 +0100787static void asus_unregister_rfkill_notifier(struct asus_wmi *asus, char *node)
Corentin Charyafa7c882011-02-06 13:28:28 +0100788{
789 acpi_status status = AE_OK;
790 acpi_handle handle;
791
792 status = acpi_get_handle(NULL, node, &handle);
793
794 if (ACPI_SUCCESS(status)) {
795 status = acpi_remove_notify_handler(handle,
Corentin Charye12e6d92011-02-26 10:20:31 +0100796 ACPI_SYSTEM_NOTIFY,
797 asus_rfkill_notify);
Corentin Charyafa7c882011-02-06 13:28:28 +0100798 if (ACPI_FAILURE(status))
799 pr_err("Error removing rfkill notify handler %s\n",
Corentin Charye12e6d92011-02-26 10:20:31 +0100800 node);
Corentin Charyafa7c882011-02-06 13:28:28 +0100801 }
802}
803
Corentin Charye12e6d92011-02-26 10:20:31 +0100804static int asus_get_adapter_status(struct hotplug_slot *hotplug_slot,
805 u8 *value)
Corentin Charyafa7c882011-02-06 13:28:28 +0100806{
Lukas Wunner125450f2018-09-08 09:59:01 +0200807 struct asus_wmi *asus = container_of(hotplug_slot,
808 struct asus_wmi, hotplug_slot);
Corentin Chary1d070f82011-02-26 10:20:36 +0100809 int result = asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_WLAN);
Corentin Charyafa7c882011-02-06 13:28:28 +0100810
Corentin Chary5c956382011-02-06 13:28:31 +0100811 if (result < 0)
812 return result;
Corentin Charyafa7c882011-02-06 13:28:28 +0100813
Corentin Chary5c956382011-02-06 13:28:31 +0100814 *value = !!result;
Corentin Charyafa7c882011-02-06 13:28:28 +0100815 return 0;
816}
817
Lukas Wunner81c4b5b2018-09-08 09:59:01 +0200818static const struct hotplug_slot_ops asus_hotplug_slot_ops = {
Corentin Charye12e6d92011-02-26 10:20:31 +0100819 .get_adapter_status = asus_get_adapter_status,
820 .get_power_status = asus_get_adapter_status,
Corentin Charyafa7c882011-02-06 13:28:28 +0100821};
822
Corentin Charye12e6d92011-02-26 10:20:31 +0100823static void asus_hotplug_work(struct work_struct *work)
Corentin Chary279f8f92011-02-06 13:28:29 +0100824{
Corentin Charye12e6d92011-02-26 10:20:31 +0100825 struct asus_wmi *asus;
Corentin Chary279f8f92011-02-06 13:28:29 +0100826
Corentin Charye12e6d92011-02-26 10:20:31 +0100827 asus = container_of(work, struct asus_wmi, hotplug_work);
828 asus_rfkill_hotplug(asus);
Corentin Chary279f8f92011-02-06 13:28:29 +0100829}
830
Corentin Charye12e6d92011-02-26 10:20:31 +0100831static int asus_setup_pci_hotplug(struct asus_wmi *asus)
Corentin Charyafa7c882011-02-06 13:28:28 +0100832{
833 int ret = -ENOMEM;
834 struct pci_bus *bus = pci_find_bus(0, 1);
835
836 if (!bus) {
837 pr_err("Unable to find wifi PCI bus\n");
838 return -ENODEV;
839 }
840
Corentin Charye12e6d92011-02-26 10:20:31 +0100841 asus->hotplug_workqueue =
842 create_singlethread_workqueue("hotplug_workqueue");
843 if (!asus->hotplug_workqueue)
Corentin Chary279f8f92011-02-06 13:28:29 +0100844 goto error_workqueue;
845
Corentin Charye12e6d92011-02-26 10:20:31 +0100846 INIT_WORK(&asus->hotplug_work, asus_hotplug_work);
Corentin Chary279f8f92011-02-06 13:28:29 +0100847
Lukas Wunner125450f2018-09-08 09:59:01 +0200848 asus->hotplug_slot.ops = &asus_hotplug_slot_ops;
Corentin Charyafa7c882011-02-06 13:28:28 +0100849
Lukas Wunner125450f2018-09-08 09:59:01 +0200850 ret = pci_hp_register(&asus->hotplug_slot, bus, 0, "asus-wifi");
Corentin Charyafa7c882011-02-06 13:28:28 +0100851 if (ret) {
852 pr_err("Unable to register hotplug slot - %d\n", ret);
853 goto error_register;
854 }
855
856 return 0;
857
858error_register:
Lukas Wunner125450f2018-09-08 09:59:01 +0200859 asus->hotplug_slot.ops = NULL;
Corentin Charye12e6d92011-02-26 10:20:31 +0100860 destroy_workqueue(asus->hotplug_workqueue);
Corentin Chary279f8f92011-02-06 13:28:29 +0100861error_workqueue:
Corentin Charyafa7c882011-02-06 13:28:28 +0100862 return ret;
863}
864
865/*
Corentin Charyba48fdb2010-11-29 08:14:07 +0100866 * Rfkill devices
867 */
Corentin Charye12e6d92011-02-26 10:20:31 +0100868static int asus_rfkill_set(void *data, bool blocked)
Corentin Charyba48fdb2010-11-29 08:14:07 +0100869{
Corentin Charya7ce3f02011-02-26 10:20:33 +0100870 struct asus_rfkill *priv = data;
Corentin Charyba48fdb2010-11-29 08:14:07 +0100871 u32 ctrl_param = !blocked;
AceLan Kaoa50bd122012-07-26 17:13:31 +0800872 u32 dev_id = priv->dev_id;
Corentin Charyba48fdb2010-11-29 08:14:07 +0100873
AceLan Kaoa50bd122012-07-26 17:13:31 +0800874 /*
875 * If the user bit is set, BIOS can't set and record the wlan status,
876 * it will report the value read from id ASUS_WMI_DEVID_WLAN_LED
877 * while we query the wlan status through WMI(ASUS_WMI_DEVID_WLAN).
878 * So, we have to record wlan status in id ASUS_WMI_DEVID_WLAN_LED
879 * while setting the wlan status through WMI.
880 * This is also the behavior that windows app will do.
881 */
882 if ((dev_id == ASUS_WMI_DEVID_WLAN) &&
883 priv->asus->driver->wlan_ctrl_by_user)
884 dev_id = ASUS_WMI_DEVID_WLAN_LED;
885
886 return asus_wmi_set_devstate(dev_id, ctrl_param, NULL);
Corentin Charyba48fdb2010-11-29 08:14:07 +0100887}
888
Corentin Charye12e6d92011-02-26 10:20:31 +0100889static void asus_rfkill_query(struct rfkill *rfkill, void *data)
Corentin Charyba48fdb2010-11-29 08:14:07 +0100890{
Corentin Charya7ce3f02011-02-26 10:20:33 +0100891 struct asus_rfkill *priv = data;
Corentin Chary5c956382011-02-06 13:28:31 +0100892 int result;
Corentin Charyba48fdb2010-11-29 08:14:07 +0100893
Corentin Chary1d070f82011-02-26 10:20:36 +0100894 result = asus_wmi_get_devstate_simple(priv->asus, priv->dev_id);
Corentin Charyba48fdb2010-11-29 08:14:07 +0100895
Corentin Chary5c956382011-02-06 13:28:31 +0100896 if (result < 0)
Corentin Charye12e6d92011-02-26 10:20:31 +0100897 return;
Corentin Charyba48fdb2010-11-29 08:14:07 +0100898
Corentin Charya7ce3f02011-02-26 10:20:33 +0100899 rfkill_set_sw_state(priv->rfkill, !result);
Corentin Charyba48fdb2010-11-29 08:14:07 +0100900}
901
Corentin Charye12e6d92011-02-26 10:20:31 +0100902static int asus_rfkill_wlan_set(void *data, bool blocked)
Corentin Chary279f8f92011-02-06 13:28:29 +0100903{
Corentin Charya7ce3f02011-02-26 10:20:33 +0100904 struct asus_rfkill *priv = data;
905 struct asus_wmi *asus = priv->asus;
Corentin Chary279f8f92011-02-06 13:28:29 +0100906 int ret;
907
908 /*
909 * This handler is enabled only if hotplug is enabled.
Corentin Charye12e6d92011-02-26 10:20:31 +0100910 * In this case, the asus_wmi_set_devstate() will
Corentin Chary279f8f92011-02-06 13:28:29 +0100911 * trigger a wmi notification and we need to wait
912 * this call to finish before being able to call
913 * any wmi method
914 */
Corentin Charye12e6d92011-02-26 10:20:31 +0100915 mutex_lock(&asus->wmi_lock);
Corentin Charya7ce3f02011-02-26 10:20:33 +0100916 ret = asus_rfkill_set(data, blocked);
Corentin Charye12e6d92011-02-26 10:20:31 +0100917 mutex_unlock(&asus->wmi_lock);
Corentin Chary279f8f92011-02-06 13:28:29 +0100918 return ret;
919}
920
Corentin Charye12e6d92011-02-26 10:20:31 +0100921static const struct rfkill_ops asus_rfkill_wlan_ops = {
922 .set_block = asus_rfkill_wlan_set,
Corentin Charya7ce3f02011-02-26 10:20:33 +0100923 .query = asus_rfkill_query,
Corentin Chary279f8f92011-02-06 13:28:29 +0100924};
925
Corentin Charye12e6d92011-02-26 10:20:31 +0100926static const struct rfkill_ops asus_rfkill_ops = {
927 .set_block = asus_rfkill_set,
928 .query = asus_rfkill_query,
Corentin Charyba48fdb2010-11-29 08:14:07 +0100929};
930
Corentin Charye12e6d92011-02-26 10:20:31 +0100931static int asus_new_rfkill(struct asus_wmi *asus,
Corentin Charya7ce3f02011-02-26 10:20:33 +0100932 struct asus_rfkill *arfkill,
Corentin Charye12e6d92011-02-26 10:20:31 +0100933 const char *name, enum rfkill_type type, int dev_id)
Corentin Charyba48fdb2010-11-29 08:14:07 +0100934{
Corentin Chary1d070f82011-02-26 10:20:36 +0100935 int result = asus_wmi_get_devstate_simple(asus, dev_id);
Corentin Charya7ce3f02011-02-26 10:20:33 +0100936 struct rfkill **rfkill = &arfkill->rfkill;
Corentin Charyba48fdb2010-11-29 08:14:07 +0100937
Corentin Chary5c956382011-02-06 13:28:31 +0100938 if (result < 0)
939 return result;
Corentin Charyba48fdb2010-11-29 08:14:07 +0100940
Corentin Charya7ce3f02011-02-26 10:20:33 +0100941 arfkill->dev_id = dev_id;
942 arfkill->asus = asus;
943
AceLan Kaoc87992d2012-03-20 09:53:08 +0100944 if (dev_id == ASUS_WMI_DEVID_WLAN &&
945 asus->driver->quirks->hotplug_wireless)
Corentin Charye12e6d92011-02-26 10:20:31 +0100946 *rfkill = rfkill_alloc(name, &asus->platform_device->dev, type,
Corentin Charya7ce3f02011-02-26 10:20:33 +0100947 &asus_rfkill_wlan_ops, arfkill);
Corentin Chary279f8f92011-02-06 13:28:29 +0100948 else
Corentin Charye12e6d92011-02-26 10:20:31 +0100949 *rfkill = rfkill_alloc(name, &asus->platform_device->dev, type,
Corentin Charya7ce3f02011-02-26 10:20:33 +0100950 &asus_rfkill_ops, arfkill);
Corentin Charyba48fdb2010-11-29 08:14:07 +0100951
952 if (!*rfkill)
953 return -EINVAL;
954
AceLan Kaoe8f56c82013-05-30 10:31:50 +0800955 if ((dev_id == ASUS_WMI_DEVID_WLAN) &&
AceLan Kaof5156232014-07-09 16:18:18 +0800956 (asus->driver->quirks->wapf > 0))
AceLan Kao6cae06e2012-07-27 16:51:59 +0800957 rfkill_set_led_trigger_name(*rfkill, "asus-wlan");
958
Corentin Chary5c956382011-02-06 13:28:31 +0100959 rfkill_init_sw_state(*rfkill, !result);
Corentin Charyba48fdb2010-11-29 08:14:07 +0100960 result = rfkill_register(*rfkill);
961 if (result) {
962 rfkill_destroy(*rfkill);
963 *rfkill = NULL;
964 return result;
965 }
966 return 0;
967}
968
Corentin Charye12e6d92011-02-26 10:20:31 +0100969static void asus_wmi_rfkill_exit(struct asus_wmi *asus)
Corentin Charyba48fdb2010-11-29 08:14:07 +0100970{
João Paulo Rechi Vitacf48bf92018-05-22 14:30:15 -0700971 if (asus->driver->wlan_ctrl_by_user && ashs_present())
972 return;
973
Corentin Charye12e6d92011-02-26 10:20:31 +0100974 asus_unregister_rfkill_notifier(asus, "\\_SB.PCI0.P0P5");
975 asus_unregister_rfkill_notifier(asus, "\\_SB.PCI0.P0P6");
976 asus_unregister_rfkill_notifier(asus, "\\_SB.PCI0.P0P7");
Corentin Charya7ce3f02011-02-26 10:20:33 +0100977 if (asus->wlan.rfkill) {
978 rfkill_unregister(asus->wlan.rfkill);
979 rfkill_destroy(asus->wlan.rfkill);
980 asus->wlan.rfkill = NULL;
Corentin Charyba48fdb2010-11-29 08:14:07 +0100981 }
Corentin Charyafa7c882011-02-06 13:28:28 +0100982 /*
983 * Refresh pci hotplug in case the rfkill state was changed after
Corentin Charye12e6d92011-02-26 10:20:31 +0100984 * asus_unregister_rfkill_notifier()
Corentin Charyafa7c882011-02-06 13:28:28 +0100985 */
Corentin Charye12e6d92011-02-26 10:20:31 +0100986 asus_rfkill_hotplug(asus);
Lukas Wunner125450f2018-09-08 09:59:01 +0200987 if (asus->hotplug_slot.ops)
988 pci_hp_deregister(&asus->hotplug_slot);
Corentin Charye12e6d92011-02-26 10:20:31 +0100989 if (asus->hotplug_workqueue)
990 destroy_workqueue(asus->hotplug_workqueue);
Corentin Charyafa7c882011-02-06 13:28:28 +0100991
Corentin Charya7ce3f02011-02-26 10:20:33 +0100992 if (asus->bluetooth.rfkill) {
993 rfkill_unregister(asus->bluetooth.rfkill);
994 rfkill_destroy(asus->bluetooth.rfkill);
995 asus->bluetooth.rfkill = NULL;
Corentin Charyba48fdb2010-11-29 08:14:07 +0100996 }
Corentin Charya7ce3f02011-02-26 10:20:33 +0100997 if (asus->wimax.rfkill) {
998 rfkill_unregister(asus->wimax.rfkill);
999 rfkill_destroy(asus->wimax.rfkill);
1000 asus->wimax.rfkill = NULL;
Corentin Chary2e9e1592011-02-06 13:28:37 +01001001 }
Corentin Charya7ce3f02011-02-26 10:20:33 +01001002 if (asus->wwan3g.rfkill) {
1003 rfkill_unregister(asus->wwan3g.rfkill);
1004 rfkill_destroy(asus->wwan3g.rfkill);
1005 asus->wwan3g.rfkill = NULL;
Corentin Charyba48fdb2010-11-29 08:14:07 +01001006 }
Corentin Chary43be8bd2011-07-01 11:34:40 +02001007 if (asus->gps.rfkill) {
1008 rfkill_unregister(asus->gps.rfkill);
1009 rfkill_destroy(asus->gps.rfkill);
1010 asus->gps.rfkill = NULL;
1011 }
Corentin Charya912d322011-07-01 11:34:41 +02001012 if (asus->uwb.rfkill) {
1013 rfkill_unregister(asus->uwb.rfkill);
1014 rfkill_destroy(asus->uwb.rfkill);
1015 asus->uwb.rfkill = NULL;
1016 }
Corentin Charyba48fdb2010-11-29 08:14:07 +01001017}
1018
Corentin Charye12e6d92011-02-26 10:20:31 +01001019static int asus_wmi_rfkill_init(struct asus_wmi *asus)
Corentin Charyba48fdb2010-11-29 08:14:07 +01001020{
1021 int result = 0;
1022
Corentin Charye12e6d92011-02-26 10:20:31 +01001023 mutex_init(&asus->hotplug_lock);
1024 mutex_init(&asus->wmi_lock);
Corentin Charyafa7c882011-02-06 13:28:28 +01001025
Corentin Charya7ce3f02011-02-26 10:20:33 +01001026 result = asus_new_rfkill(asus, &asus->wlan, "asus-wlan",
1027 RFKILL_TYPE_WLAN, ASUS_WMI_DEVID_WLAN);
Corentin Charyba48fdb2010-11-29 08:14:07 +01001028
1029 if (result && result != -ENODEV)
1030 goto exit;
1031
Corentin Charya7ce3f02011-02-26 10:20:33 +01001032 result = asus_new_rfkill(asus, &asus->bluetooth,
Corentin Charye12e6d92011-02-26 10:20:31 +01001033 "asus-bluetooth", RFKILL_TYPE_BLUETOOTH,
1034 ASUS_WMI_DEVID_BLUETOOTH);
Corentin Charyba48fdb2010-11-29 08:14:07 +01001035
1036 if (result && result != -ENODEV)
1037 goto exit;
1038
Corentin Charya7ce3f02011-02-26 10:20:33 +01001039 result = asus_new_rfkill(asus, &asus->wimax, "asus-wimax",
1040 RFKILL_TYPE_WIMAX, ASUS_WMI_DEVID_WIMAX);
Corentin Chary2e9e1592011-02-06 13:28:37 +01001041
1042 if (result && result != -ENODEV)
1043 goto exit;
1044
Corentin Charya7ce3f02011-02-26 10:20:33 +01001045 result = asus_new_rfkill(asus, &asus->wwan3g, "asus-wwan3g",
1046 RFKILL_TYPE_WWAN, ASUS_WMI_DEVID_WWAN3G);
Corentin Charyba48fdb2010-11-29 08:14:07 +01001047
1048 if (result && result != -ENODEV)
1049 goto exit;
1050
Corentin Chary43be8bd2011-07-01 11:34:40 +02001051 result = asus_new_rfkill(asus, &asus->gps, "asus-gps",
1052 RFKILL_TYPE_GPS, ASUS_WMI_DEVID_GPS);
1053
1054 if (result && result != -ENODEV)
1055 goto exit;
1056
Corentin Charya912d322011-07-01 11:34:41 +02001057 result = asus_new_rfkill(asus, &asus->uwb, "asus-uwb",
1058 RFKILL_TYPE_UWB, ASUS_WMI_DEVID_UWB);
1059
1060 if (result && result != -ENODEV)
1061 goto exit;
1062
AceLan Kaoc87992d2012-03-20 09:53:08 +01001063 if (!asus->driver->quirks->hotplug_wireless)
Corentin Charyc14d4b82011-02-06 13:28:40 +01001064 goto exit;
1065
Corentin Charye12e6d92011-02-26 10:20:31 +01001066 result = asus_setup_pci_hotplug(asus);
Corentin Charyafa7c882011-02-06 13:28:28 +01001067 /*
1068 * If we get -EBUSY then something else is handling the PCI hotplug -
1069 * don't fail in this case
1070 */
1071 if (result == -EBUSY)
1072 result = 0;
1073
Corentin Charye12e6d92011-02-26 10:20:31 +01001074 asus_register_rfkill_notifier(asus, "\\_SB.PCI0.P0P5");
1075 asus_register_rfkill_notifier(asus, "\\_SB.PCI0.P0P6");
1076 asus_register_rfkill_notifier(asus, "\\_SB.PCI0.P0P7");
Corentin Charyafa7c882011-02-06 13:28:28 +01001077 /*
1078 * Refresh pci hotplug in case the rfkill state was changed during
1079 * setup.
1080 */
Corentin Charye12e6d92011-02-26 10:20:31 +01001081 asus_rfkill_hotplug(asus);
Corentin Charyafa7c882011-02-06 13:28:28 +01001082
Corentin Charyba48fdb2010-11-29 08:14:07 +01001083exit:
1084 if (result && result != -ENODEV)
Corentin Charye12e6d92011-02-26 10:20:31 +01001085 asus_wmi_rfkill_exit(asus);
Corentin Charyba48fdb2010-11-29 08:14:07 +01001086
1087 if (result == -ENODEV)
1088 result = 0;
1089
1090 return result;
1091}
1092
Yurii Pavlovskyi54a31212019-05-14 21:04:48 +02001093/* Quirks *********************************************************************/
1094
Kai-Chuan Hsieh8023eff2016-09-01 23:55:55 +08001095static void asus_wmi_set_xusb2pr(struct asus_wmi *asus)
1096{
1097 struct pci_dev *xhci_pdev;
1098 u32 orig_ports_available;
1099 u32 ports_available = asus->driver->quirks->xusb2pr;
1100
1101 xhci_pdev = pci_get_device(PCI_VENDOR_ID_INTEL,
1102 PCI_DEVICE_ID_INTEL_LYNXPOINT_LP_XHCI,
1103 NULL);
1104
1105 if (!xhci_pdev)
1106 return;
1107
1108 pci_read_config_dword(xhci_pdev, USB_INTEL_XUSB2PR,
1109 &orig_ports_available);
1110
1111 pci_write_config_dword(xhci_pdev, USB_INTEL_XUSB2PR,
1112 cpu_to_le32(ports_available));
1113
1114 pr_info("set USB_INTEL_XUSB2PR old: 0x%04x, new: 0x%04x\n",
1115 orig_ports_available, ports_available);
1116}
1117
Corentin Charyba48fdb2010-11-29 08:14:07 +01001118/*
Oleksij Rempele9b61512017-04-28 16:19:49 +02001119 * Some devices dont support or have borcken get_als method
1120 * but still support set method.
1121 */
1122static void asus_wmi_set_als(void)
1123{
1124 asus_wmi_set_devstate(ASUS_WMI_DEVID_ALS_ENABLE, 1, NULL);
1125}
1126
Yurii Pavlovskyi54a31212019-05-14 21:04:48 +02001127/* Hwmon device ***************************************************************/
1128
Kast Bernd53e755c2015-05-13 16:24:16 +02001129static int asus_hwmon_agfn_fan_speed_read(struct asus_wmi *asus, int fan,
1130 int *speed)
1131{
1132 struct fan_args args = {
1133 .agfn.len = sizeof(args),
1134 .agfn.mfun = ASUS_FAN_MFUN,
1135 .agfn.sfun = ASUS_FAN_SFUN_READ,
1136 .fan = fan,
1137 .speed = 0,
1138 };
1139 struct acpi_buffer input = { (acpi_size) sizeof(args), &args };
1140 int status;
1141
1142 if (fan != 1)
1143 return -EINVAL;
1144
1145 status = asus_wmi_evaluate_method_agfn(input);
1146
1147 if (status || args.agfn.err)
1148 return -ENXIO;
1149
1150 if (speed)
1151 *speed = args.speed;
1152
1153 return 0;
1154}
1155
1156static int asus_hwmon_agfn_fan_speed_write(struct asus_wmi *asus, int fan,
1157 int *speed)
1158{
1159 struct fan_args args = {
1160 .agfn.len = sizeof(args),
1161 .agfn.mfun = ASUS_FAN_MFUN,
1162 .agfn.sfun = ASUS_FAN_SFUN_WRITE,
1163 .fan = fan,
1164 .speed = speed ? *speed : 0,
1165 };
1166 struct acpi_buffer input = { (acpi_size) sizeof(args), &args };
1167 int status;
1168
1169 /* 1: for setting 1st fan's speed 0: setting auto mode */
1170 if (fan != 1 && fan != 0)
1171 return -EINVAL;
1172
1173 status = asus_wmi_evaluate_method_agfn(input);
1174
1175 if (status || args.agfn.err)
1176 return -ENXIO;
1177
1178 if (speed && fan == 1)
1179 asus->asus_hwmon_pwm = *speed;
1180
1181 return 0;
1182}
1183
1184/*
1185 * Check if we can read the speed of one fan. If true we assume we can also
1186 * control it.
1187 */
1188static int asus_hwmon_get_fan_number(struct asus_wmi *asus, int *num_fans)
1189{
1190 int status;
1191 int speed = 0;
1192
1193 *num_fans = 0;
1194
1195 status = asus_hwmon_agfn_fan_speed_read(asus, 1, &speed);
1196 if (!status)
1197 *num_fans = 1;
1198
1199 return 0;
1200}
1201
1202static int asus_hwmon_fan_set_auto(struct asus_wmi *asus)
1203{
1204 int status;
1205
1206 status = asus_hwmon_agfn_fan_speed_write(asus, 0, NULL);
1207 if (status)
1208 return -ENXIO;
1209
1210 asus->asus_hwmon_fan_manual_mode = false;
1211
1212 return 0;
1213}
1214
1215static int asus_hwmon_fan_rpm_show(struct device *dev, int fan)
1216{
1217 struct asus_wmi *asus = dev_get_drvdata(dev);
1218 int value;
1219 int ret;
1220
1221 /* no speed readable on manual mode */
1222 if (asus->asus_hwmon_fan_manual_mode)
1223 return -ENXIO;
1224
1225 ret = asus_hwmon_agfn_fan_speed_read(asus, fan+1, &value);
1226 if (ret) {
1227 pr_warn("reading fan speed failed: %d\n", ret);
1228 return -ENXIO;
1229 }
1230
1231 return value;
1232}
1233
1234static void asus_hwmon_pwm_show(struct asus_wmi *asus, int fan, int *value)
1235{
1236 int err;
1237
1238 if (asus->asus_hwmon_pwm >= 0) {
1239 *value = asus->asus_hwmon_pwm;
1240 return;
1241 }
1242
1243 err = asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_FAN_CTRL, value);
1244 if (err < 0)
1245 return;
1246
1247 *value &= 0xFF;
1248
1249 if (*value == 1) /* Low Speed */
1250 *value = 85;
1251 else if (*value == 2)
1252 *value = 170;
1253 else if (*value == 3)
1254 *value = 255;
1255 else if (*value) {
1256 pr_err("Unknown fan speed %#x\n", *value);
1257 *value = -1;
1258 }
1259}
1260
1261static ssize_t pwm1_show(struct device *dev,
Corentin Chary49979d02011-07-01 11:34:26 +02001262 struct device_attribute *attr,
1263 char *buf)
Corentin Charye07babd2011-02-26 10:20:42 +01001264{
1265 struct asus_wmi *asus = dev_get_drvdata(dev);
Kast Bernd53e755c2015-05-13 16:24:16 +02001266 int value;
Corentin Charye07babd2011-02-26 10:20:42 +01001267
Kast Bernd53e755c2015-05-13 16:24:16 +02001268 asus_hwmon_pwm_show(asus, 0, &value);
Corentin Charye07babd2011-02-26 10:20:42 +01001269
1270 return sprintf(buf, "%d\n", value);
1271}
1272
Kast Bernd53e755c2015-05-13 16:24:16 +02001273static ssize_t pwm1_store(struct device *dev,
1274 struct device_attribute *attr,
1275 const char *buf, size_t count) {
1276 struct asus_wmi *asus = dev_get_drvdata(dev);
1277 int value;
1278 int state;
1279 int ret;
1280
1281 ret = kstrtouint(buf, 10, &value);
1282
1283 if (ret)
1284 return ret;
1285
1286 value = clamp(value, 0, 255);
1287
1288 state = asus_hwmon_agfn_fan_speed_write(asus, 1, &value);
1289 if (state)
1290 pr_warn("Setting fan speed failed: %d\n", state);
1291 else
1292 asus->asus_hwmon_fan_manual_mode = true;
1293
1294 return count;
1295}
1296
1297static ssize_t fan1_input_show(struct device *dev,
1298 struct device_attribute *attr,
1299 char *buf)
1300{
1301 int value = asus_hwmon_fan_rpm_show(dev, 0);
1302
1303 return sprintf(buf, "%d\n", value < 0 ? -1 : value*100);
1304
1305}
1306
1307static ssize_t pwm1_enable_show(struct device *dev,
1308 struct device_attribute *attr,
1309 char *buf)
1310{
1311 struct asus_wmi *asus = dev_get_drvdata(dev);
1312
1313 if (asus->asus_hwmon_fan_manual_mode)
1314 return sprintf(buf, "%d\n", ASUS_FAN_CTRL_MANUAL);
1315
1316 return sprintf(buf, "%d\n", ASUS_FAN_CTRL_AUTO);
1317}
1318
1319static ssize_t pwm1_enable_store(struct device *dev,
1320 struct device_attribute *attr,
1321 const char *buf, size_t count)
1322{
1323 struct asus_wmi *asus = dev_get_drvdata(dev);
1324 int status = 0;
1325 int state;
1326 int ret;
1327
1328 ret = kstrtouint(buf, 10, &state);
1329
1330 if (ret)
1331 return ret;
1332
1333 if (state == ASUS_FAN_CTRL_MANUAL)
1334 asus->asus_hwmon_fan_manual_mode = true;
1335 else
1336 status = asus_hwmon_fan_set_auto(asus);
1337
1338 if (status)
1339 return status;
1340
1341 return count;
1342}
1343
1344static ssize_t fan1_label_show(struct device *dev,
1345 struct device_attribute *attr,
1346 char *buf)
1347{
1348 return sprintf(buf, "%s\n", ASUS_FAN_DESC);
1349}
1350
Corentin Chary6118b8a2011-07-01 11:34:36 +02001351static ssize_t asus_hwmon_temp1(struct device *dev,
1352 struct device_attribute *attr,
1353 char *buf)
1354{
1355 struct asus_wmi *asus = dev_get_drvdata(dev);
1356 u32 value;
1357 int err;
1358
1359 err = asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_THERMAL_CTRL, &value);
1360
1361 if (err < 0)
1362 return err;
1363
Rasmus Villemoese866a2e2015-10-01 23:45:31 +02001364 value = DECI_KELVIN_TO_CELSIUS((value & 0xFFFF)) * 1000;
Corentin Chary6118b8a2011-07-01 11:34:36 +02001365
1366 return sprintf(buf, "%d\n", value);
1367}
1368
Kast Bernd53e755c2015-05-13 16:24:16 +02001369/* Fan1 */
1370static DEVICE_ATTR_RW(pwm1);
1371static DEVICE_ATTR_RW(pwm1_enable);
1372static DEVICE_ATTR_RO(fan1_input);
1373static DEVICE_ATTR_RO(fan1_label);
1374
1375/* Temperature */
Guenter Roeck50a639f2013-11-23 11:03:17 -08001376static DEVICE_ATTR(temp1_input, S_IRUGO, asus_hwmon_temp1, NULL);
Corentin Charye07babd2011-02-26 10:20:42 +01001377
1378static struct attribute *hwmon_attributes[] = {
Guenter Roeck50a639f2013-11-23 11:03:17 -08001379 &dev_attr_pwm1.attr,
Kast Bernd53e755c2015-05-13 16:24:16 +02001380 &dev_attr_pwm1_enable.attr,
1381 &dev_attr_fan1_input.attr,
1382 &dev_attr_fan1_label.attr,
1383
Guenter Roeck50a639f2013-11-23 11:03:17 -08001384 &dev_attr_temp1_input.attr,
Corentin Charye07babd2011-02-26 10:20:42 +01001385 NULL
1386};
1387
Al Viro587a1f12011-07-23 23:11:19 -04001388static umode_t asus_hwmon_sysfs_is_visible(struct kobject *kobj,
Corentin Charye02431d2011-07-01 11:34:37 +02001389 struct attribute *attr, int idx)
Corentin Charye07babd2011-02-26 10:20:42 +01001390{
1391 struct device *dev = container_of(kobj, struct device, kobj);
Fuqian Huang8e8fe442019-07-01 11:24:26 +08001392 struct asus_wmi *asus = dev_get_drvdata(dev->parent);
Corentin Charye07babd2011-02-26 10:20:42 +01001393 int dev_id = -1;
Kast Bernd53e755c2015-05-13 16:24:16 +02001394 int fan_attr = -1;
Corentin Charye07babd2011-02-26 10:20:42 +01001395 u32 value = ASUS_WMI_UNSUPPORTED_METHOD;
Kast Bernd53e755c2015-05-13 16:24:16 +02001396 bool ok = true;
Corentin Charye07babd2011-02-26 10:20:42 +01001397
Guenter Roeck50a639f2013-11-23 11:03:17 -08001398 if (attr == &dev_attr_pwm1.attr)
Corentin Charye07babd2011-02-26 10:20:42 +01001399 dev_id = ASUS_WMI_DEVID_FAN_CTRL;
Guenter Roeck50a639f2013-11-23 11:03:17 -08001400 else if (attr == &dev_attr_temp1_input.attr)
Corentin Charye02431d2011-07-01 11:34:37 +02001401 dev_id = ASUS_WMI_DEVID_THERMAL_CTRL;
Corentin Charye07babd2011-02-26 10:20:42 +01001402
Kast Bernd53e755c2015-05-13 16:24:16 +02001403
1404 if (attr == &dev_attr_fan1_input.attr
1405 || attr == &dev_attr_fan1_label.attr
1406 || attr == &dev_attr_pwm1.attr
1407 || attr == &dev_attr_pwm1_enable.attr) {
1408 fan_attr = 1;
1409 }
1410
Corentin Charye07babd2011-02-26 10:20:42 +01001411 if (dev_id != -1) {
1412 int err = asus_wmi_get_devstate(asus, dev_id, &value);
1413
Kast Bernd53e755c2015-05-13 16:24:16 +02001414 if (err < 0 && fan_attr == -1)
Al Viroe772aed2011-07-23 20:59:40 -04001415 return 0; /* can't return negative here */
Corentin Charye07babd2011-02-26 10:20:42 +01001416 }
1417
1418 if (dev_id == ASUS_WMI_DEVID_FAN_CTRL) {
1419 /*
1420 * We need to find a better way, probably using sfun,
1421 * bits or spec ...
1422 * Currently we disable it if:
1423 * - ASUS_WMI_UNSUPPORTED_METHOD is returned
1424 * - reverved bits are non-zero
1425 * - sfun and presence bit are not set
1426 */
Corentin Chary49979d02011-07-01 11:34:26 +02001427 if (value == ASUS_WMI_UNSUPPORTED_METHOD || value & 0xFFF80000
Corentin Charye07babd2011-02-26 10:20:42 +01001428 || (!asus->sfun && !(value & ASUS_WMI_DSTS_PRESENCE_BIT)))
1429 ok = false;
Kast Bernd53e755c2015-05-13 16:24:16 +02001430 else
1431 ok = fan_attr <= asus->asus_hwmon_num_fans;
Corentin Charye02431d2011-07-01 11:34:37 +02001432 } else if (dev_id == ASUS_WMI_DEVID_THERMAL_CTRL) {
Yurii Pavlovskyi4fd19822019-05-14 21:05:46 +02001433 /*
1434 * If the temperature value in deci-Kelvin is near the absolute
1435 * zero temperature, something is clearly wrong
1436 */
1437 if (value == 0 || value == 1)
Corentin Charye02431d2011-07-01 11:34:37 +02001438 ok = false;
Kast Bernd53e755c2015-05-13 16:24:16 +02001439 } else if (fan_attr <= asus->asus_hwmon_num_fans && fan_attr != -1) {
1440 ok = true;
1441 } else {
1442 ok = false;
Corentin Charye07babd2011-02-26 10:20:42 +01001443 }
1444
1445 return ok ? attr->mode : 0;
1446}
1447
Arvind Yadave90d9ba2017-07-11 16:18:19 +05301448static const struct attribute_group hwmon_attribute_group = {
Corentin Charye07babd2011-02-26 10:20:42 +01001449 .is_visible = asus_hwmon_sysfs_is_visible,
1450 .attrs = hwmon_attributes
1451};
Guenter Roeck50a639f2013-11-23 11:03:17 -08001452__ATTRIBUTE_GROUPS(hwmon_attribute);
Corentin Charye07babd2011-02-26 10:20:42 +01001453
1454static int asus_wmi_hwmon_init(struct asus_wmi *asus)
1455{
Yurii Pavlovskyicd10ee02019-05-14 20:50:30 +02001456 struct device *dev = &asus->platform_device->dev;
Corentin Charye07babd2011-02-26 10:20:42 +01001457 struct device *hwmon;
Corentin Charye07babd2011-02-26 10:20:42 +01001458
Yurii Pavlovskyicd10ee02019-05-14 20:50:30 +02001459 hwmon = devm_hwmon_device_register_with_groups(dev, "asus", asus,
1460 hwmon_attribute_groups);
1461
Corentin Charye07babd2011-02-26 10:20:42 +01001462 if (IS_ERR(hwmon)) {
1463 pr_err("Could not register asus hwmon device\n");
1464 return PTR_ERR(hwmon);
1465 }
Guenter Roeck50a639f2013-11-23 11:03:17 -08001466 return 0;
Corentin Charye07babd2011-02-26 10:20:42 +01001467}
1468
Yurii Pavlovskyi54a31212019-05-14 21:04:48 +02001469static int asus_wmi_fan_init(struct asus_wmi *asus)
1470{
1471 int status;
1472
1473 asus->asus_hwmon_pwm = -1;
1474 asus->asus_hwmon_num_fans = -1;
1475 asus->asus_hwmon_fan_manual_mode = false;
1476
1477 status = asus_hwmon_get_fan_number(asus, &asus->asus_hwmon_num_fans);
1478 if (status) {
1479 asus->asus_hwmon_num_fans = 0;
1480 pr_warn("Could not determine number of fans: %d\n", status);
1481 return -ENXIO;
1482 }
1483
1484 pr_info("Number of fans: %d\n", asus->asus_hwmon_num_fans);
1485 return 0;
1486}
1487
Yurii Pavlovskyib096f622019-05-14 21:07:05 +02001488/* Fan mode *******************************************************************/
1489
Daniel Drake9af93db2019-07-17 13:10:58 +08001490static int fan_boost_mode_check_present(struct asus_wmi *asus)
Yurii Pavlovskyib096f622019-05-14 21:07:05 +02001491{
1492 u32 result;
1493 int err;
1494
Daniel Drake9af93db2019-07-17 13:10:58 +08001495 asus->fan_boost_mode_available = false;
Yurii Pavlovskyib096f622019-05-14 21:07:05 +02001496
Daniel Drake9af93db2019-07-17 13:10:58 +08001497 err = asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_FAN_BOOST_MODE,
1498 &result);
Yurii Pavlovskyib096f622019-05-14 21:07:05 +02001499 if (err) {
1500 if (err == -ENODEV)
1501 return 0;
1502 else
1503 return err;
1504 }
1505
1506 if ((result & ASUS_WMI_DSTS_PRESENCE_BIT) &&
Daniel Drake9af93db2019-07-17 13:10:58 +08001507 (result & ASUS_FAN_BOOST_MODES_MASK)) {
1508 asus->fan_boost_mode_available = true;
1509 asus->fan_boost_mode_mask = result & ASUS_FAN_BOOST_MODES_MASK;
Yurii Pavlovskyib096f622019-05-14 21:07:05 +02001510 }
1511
1512 return 0;
1513}
1514
Daniel Drake9af93db2019-07-17 13:10:58 +08001515static int fan_boost_mode_write(struct asus_wmi *asus)
Yurii Pavlovskyib096f622019-05-14 21:07:05 +02001516{
1517 int err;
1518 u8 value;
1519 u32 retval;
1520
Daniel Drake9af93db2019-07-17 13:10:58 +08001521 value = asus->fan_boost_mode;
Yurii Pavlovskyib096f622019-05-14 21:07:05 +02001522
Daniel Drake9af93db2019-07-17 13:10:58 +08001523 pr_info("Set fan boost mode: %u\n", value);
1524 err = asus_wmi_set_devstate(ASUS_WMI_DEVID_FAN_BOOST_MODE, value,
1525 &retval);
Yurii Pavlovskyib096f622019-05-14 21:07:05 +02001526
1527 if (err) {
Daniel Drake9af93db2019-07-17 13:10:58 +08001528 pr_warn("Failed to set fan boost mode: %d\n", err);
Yurii Pavlovskyib096f622019-05-14 21:07:05 +02001529 return err;
1530 }
1531
1532 if (retval != 1) {
Daniel Drake9af93db2019-07-17 13:10:58 +08001533 pr_warn("Failed to set fan boost mode (retval): 0x%x\n",
1534 retval);
Yurii Pavlovskyib096f622019-05-14 21:07:05 +02001535 return -EIO;
1536 }
1537
1538 return 0;
1539}
1540
Daniel Drake9af93db2019-07-17 13:10:58 +08001541static int fan_boost_mode_switch_next(struct asus_wmi *asus)
Yurii Pavlovskyib096f622019-05-14 21:07:05 +02001542{
Daniel Drake9af93db2019-07-17 13:10:58 +08001543 u8 mask = asus->fan_boost_mode_mask;
1544
1545 if (asus->fan_boost_mode == ASUS_FAN_BOOST_MODE_NORMAL) {
1546 if (mask & ASUS_FAN_BOOST_MODE_OVERBOOST_MASK)
1547 asus->fan_boost_mode = ASUS_FAN_BOOST_MODE_OVERBOOST;
1548 else if (mask & ASUS_FAN_BOOST_MODE_SILENT_MASK)
1549 asus->fan_boost_mode = ASUS_FAN_BOOST_MODE_SILENT;
1550 } else if (asus->fan_boost_mode == ASUS_FAN_BOOST_MODE_OVERBOOST) {
1551 if (mask & ASUS_FAN_BOOST_MODE_SILENT_MASK)
1552 asus->fan_boost_mode = ASUS_FAN_BOOST_MODE_SILENT;
Yurii Pavlovskyib096f622019-05-14 21:07:05 +02001553 else
Daniel Drake9af93db2019-07-17 13:10:58 +08001554 asus->fan_boost_mode = ASUS_FAN_BOOST_MODE_NORMAL;
Yurii Pavlovskyib096f622019-05-14 21:07:05 +02001555 } else {
Daniel Drake9af93db2019-07-17 13:10:58 +08001556 asus->fan_boost_mode = ASUS_FAN_BOOST_MODE_NORMAL;
Yurii Pavlovskyib096f622019-05-14 21:07:05 +02001557 }
1558
Daniel Drake9af93db2019-07-17 13:10:58 +08001559 return fan_boost_mode_write(asus);
Yurii Pavlovskyib096f622019-05-14 21:07:05 +02001560}
1561
Daniel Drake9af93db2019-07-17 13:10:58 +08001562static ssize_t fan_boost_mode_show(struct device *dev,
1563 struct device_attribute *attr, char *buf)
Yurii Pavlovskyib096f622019-05-14 21:07:05 +02001564{
1565 struct asus_wmi *asus = dev_get_drvdata(dev);
1566
Daniel Drake9af93db2019-07-17 13:10:58 +08001567 return scnprintf(buf, PAGE_SIZE, "%d\n", asus->fan_boost_mode);
Yurii Pavlovskyib096f622019-05-14 21:07:05 +02001568}
1569
Daniel Drake9af93db2019-07-17 13:10:58 +08001570static ssize_t fan_boost_mode_store(struct device *dev,
1571 struct device_attribute *attr,
1572 const char *buf, size_t count)
Yurii Pavlovskyib096f622019-05-14 21:07:05 +02001573{
1574 int result;
1575 u8 new_mode;
Yurii Pavlovskyib096f622019-05-14 21:07:05 +02001576 struct asus_wmi *asus = dev_get_drvdata(dev);
Daniel Drake9af93db2019-07-17 13:10:58 +08001577 u8 mask = asus->fan_boost_mode_mask;
Yurii Pavlovskyib096f622019-05-14 21:07:05 +02001578
1579 result = kstrtou8(buf, 10, &new_mode);
1580 if (result < 0) {
1581 pr_warn("Trying to store invalid value\n");
1582 return result;
1583 }
1584
Daniel Drake9af93db2019-07-17 13:10:58 +08001585 if (new_mode == ASUS_FAN_BOOST_MODE_OVERBOOST) {
1586 if (!(mask & ASUS_FAN_BOOST_MODE_OVERBOOST_MASK))
Yurii Pavlovskyib096f622019-05-14 21:07:05 +02001587 return -EINVAL;
Daniel Drake9af93db2019-07-17 13:10:58 +08001588 } else if (new_mode == ASUS_FAN_BOOST_MODE_SILENT) {
1589 if (!(mask & ASUS_FAN_BOOST_MODE_SILENT_MASK))
Yurii Pavlovskyib096f622019-05-14 21:07:05 +02001590 return -EINVAL;
Daniel Drake9af93db2019-07-17 13:10:58 +08001591 } else if (new_mode != ASUS_FAN_BOOST_MODE_NORMAL) {
Yurii Pavlovskyib096f622019-05-14 21:07:05 +02001592 return -EINVAL;
1593 }
1594
Daniel Drake9af93db2019-07-17 13:10:58 +08001595 asus->fan_boost_mode = new_mode;
1596 fan_boost_mode_write(asus);
Yurii Pavlovskyib096f622019-05-14 21:07:05 +02001597
1598 return result;
1599}
1600
Daniel Drake9af93db2019-07-17 13:10:58 +08001601// Fan boost mode: 0 - normal, 1 - overboost, 2 - silent
1602static DEVICE_ATTR_RW(fan_boost_mode);
Yurii Pavlovskyib096f622019-05-14 21:07:05 +02001603
Yurii Pavlovskyi54a31212019-05-14 21:04:48 +02001604/* Backlight ******************************************************************/
1605
Corentin Chary1d070f82011-02-26 10:20:36 +01001606static int read_backlight_power(struct asus_wmi *asus)
Corentin Charyb7187262011-02-06 13:28:39 +01001607{
AceLan Kao6e0044b2012-03-20 09:53:09 +01001608 int ret;
1609 if (asus->driver->quirks->store_backlight_power)
1610 ret = !asus->driver->panel_power;
1611 else
1612 ret = asus_wmi_get_devstate_simple(asus,
1613 ASUS_WMI_DEVID_BACKLIGHT);
Corentin Charyb7187262011-02-06 13:28:39 +01001614
1615 if (ret < 0)
1616 return ret;
1617
1618 return ret ? FB_BLANK_UNBLANK : FB_BLANK_POWERDOWN;
1619}
1620
Corentin Chary8fbea012011-02-26 10:20:37 +01001621static int read_brightness_max(struct asus_wmi *asus)
1622{
1623 u32 retval;
1624 int err;
1625
1626 err = asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_BRIGHTNESS, &retval);
1627
1628 if (err < 0)
1629 return err;
1630
1631 retval = retval & ASUS_WMI_DSTS_MAX_BRIGTH_MASK;
1632 retval >>= 8;
1633
1634 if (!retval)
1635 return -ENODEV;
1636
1637 return retval;
1638}
1639
Yong Wang3d7b1652010-04-11 09:27:54 +08001640static int read_brightness(struct backlight_device *bd)
1641{
Corentin Chary1d070f82011-02-26 10:20:36 +01001642 struct asus_wmi *asus = bl_get_data(bd);
Dan Carpenter0986f252011-03-15 10:06:23 +03001643 u32 retval;
1644 int err;
Yong Wang3d7b1652010-04-11 09:27:54 +08001645
Corentin Chary1d070f82011-02-26 10:20:36 +01001646 err = asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_BRIGHTNESS, &retval);
Yong Wang3d7b1652010-04-11 09:27:54 +08001647
Corentin Charyd33da3b2011-02-26 10:20:35 +01001648 if (err < 0)
1649 return err;
1650
1651 return retval & ASUS_WMI_DSTS_BRIGHTNESS_MASK;
Yong Wang3d7b1652010-04-11 09:27:54 +08001652}
1653
AceLan Kaoc87992d2012-03-20 09:53:08 +01001654static u32 get_scalar_command(struct backlight_device *bd)
1655{
1656 struct asus_wmi *asus = bl_get_data(bd);
1657 u32 ctrl_param = 0;
1658
1659 if ((asus->driver->brightness < bd->props.brightness) ||
1660 bd->props.brightness == bd->props.max_brightness)
1661 ctrl_param = 0x00008001;
1662 else if ((asus->driver->brightness > bd->props.brightness) ||
1663 bd->props.brightness == 0)
1664 ctrl_param = 0x00008000;
1665
1666 asus->driver->brightness = bd->props.brightness;
1667
1668 return ctrl_param;
1669}
1670
Yong Wang3d7b1652010-04-11 09:27:54 +08001671static int update_bl_status(struct backlight_device *bd)
1672{
Corentin Chary1d070f82011-02-26 10:20:36 +01001673 struct asus_wmi *asus = bl_get_data(bd);
Corentin Charydfed65d2010-11-29 08:14:12 +01001674 u32 ctrl_param;
AceLan Kao6e0044b2012-03-20 09:53:09 +01001675 int power, err = 0;
Corentin Charyb7187262011-02-06 13:28:39 +01001676
Corentin Chary1d070f82011-02-26 10:20:36 +01001677 power = read_backlight_power(asus);
Corentin Charyb7187262011-02-06 13:28:39 +01001678 if (power != -ENODEV && bd->props.power != power) {
1679 ctrl_param = !!(bd->props.power == FB_BLANK_UNBLANK);
Corentin Charyd33da3b2011-02-26 10:20:35 +01001680 err = asus_wmi_set_devstate(ASUS_WMI_DEVID_BACKLIGHT,
1681 ctrl_param, NULL);
AceLan Kao6e0044b2012-03-20 09:53:09 +01001682 if (asus->driver->quirks->store_backlight_power)
1683 asus->driver->panel_power = bd->props.power;
AceLan Kao6e0044b2012-03-20 09:53:09 +01001684
Corentin Charyade28ab2012-03-20 09:53:14 +01001685 /* When using scalar brightness, updating the brightness
1686 * will mess with the backlight power */
1687 if (asus->driver->quirks->scalar_panel_brightness)
1688 return err;
Corentin Charyb7187262011-02-06 13:28:39 +01001689 }
Corentin Charyade28ab2012-03-20 09:53:14 +01001690
1691 if (asus->driver->quirks->scalar_panel_brightness)
1692 ctrl_param = get_scalar_command(bd);
1693 else
1694 ctrl_param = bd->props.brightness;
1695
1696 err = asus_wmi_set_devstate(ASUS_WMI_DEVID_BRIGHTNESS,
1697 ctrl_param, NULL);
1698
Corentin Chary8fbea012011-02-26 10:20:37 +01001699 return err;
Yong Wang3d7b1652010-04-11 09:27:54 +08001700}
1701
Corentin Charye12e6d92011-02-26 10:20:31 +01001702static const struct backlight_ops asus_wmi_bl_ops = {
Yong Wang3d7b1652010-04-11 09:27:54 +08001703 .get_brightness = read_brightness,
1704 .update_status = update_bl_status,
1705};
1706
Corentin Charye12e6d92011-02-26 10:20:31 +01001707static int asus_wmi_backlight_notify(struct asus_wmi *asus, int code)
Yong Wang3d7b1652010-04-11 09:27:54 +08001708{
Corentin Charye12e6d92011-02-26 10:20:31 +01001709 struct backlight_device *bd = asus->backlight_device;
Yong Wang3d7b1652010-04-11 09:27:54 +08001710 int old = bd->props.brightness;
Daniel Mackb7670ed2010-05-19 12:37:01 +02001711 int new = old;
Yong Wang3d7b1652010-04-11 09:27:54 +08001712
1713 if (code >= NOTIFY_BRNUP_MIN && code <= NOTIFY_BRNUP_MAX)
1714 new = code - NOTIFY_BRNUP_MIN + 1;
1715 else if (code >= NOTIFY_BRNDOWN_MIN && code <= NOTIFY_BRNDOWN_MAX)
1716 new = code - NOTIFY_BRNDOWN_MIN;
1717
1718 bd->props.brightness = new;
1719 backlight_update_status(bd);
1720 backlight_force_update(bd, BACKLIGHT_UPDATE_HOTKEY);
1721
1722 return old;
1723}
1724
Corentin Charye12e6d92011-02-26 10:20:31 +01001725static int asus_wmi_backlight_init(struct asus_wmi *asus)
Yong Wang3d7b1652010-04-11 09:27:54 +08001726{
1727 struct backlight_device *bd;
1728 struct backlight_properties props;
Corentin Charyb7187262011-02-06 13:28:39 +01001729 int max;
1730 int power;
1731
Corentin Chary8fbea012011-02-26 10:20:37 +01001732 max = read_brightness_max(asus);
Hans de Goede86ac27352014-07-08 10:47:22 +02001733 if (max < 0)
Corentin Chary8fbea012011-02-26 10:20:37 +01001734 return max;
1735
1736 power = read_backlight_power(asus);
1737
Corentin Charyb7187262011-02-06 13:28:39 +01001738 if (power == -ENODEV)
1739 power = FB_BLANK_UNBLANK;
Corentin Chary8fbea012011-02-26 10:20:37 +01001740 else if (power < 0)
1741 return power;
Yong Wang3d7b1652010-04-11 09:27:54 +08001742
1743 memset(&props, 0, sizeof(struct backlight_properties));
Axel Lin60cfa092011-06-29 11:43:30 +08001744 props.type = BACKLIGHT_PLATFORM;
Corentin Charyb7187262011-02-06 13:28:39 +01001745 props.max_brightness = max;
Corentin Charye12e6d92011-02-26 10:20:31 +01001746 bd = backlight_device_register(asus->driver->name,
1747 &asus->platform_device->dev, asus,
1748 &asus_wmi_bl_ops, &props);
Yong Wang3d7b1652010-04-11 09:27:54 +08001749 if (IS_ERR(bd)) {
1750 pr_err("Could not register backlight device\n");
1751 return PTR_ERR(bd);
1752 }
1753
Corentin Charye12e6d92011-02-26 10:20:31 +01001754 asus->backlight_device = bd;
Yong Wang3d7b1652010-04-11 09:27:54 +08001755
AceLan Kao6e0044b2012-03-20 09:53:09 +01001756 if (asus->driver->quirks->store_backlight_power)
1757 asus->driver->panel_power = power;
1758
Yong Wang3d7b1652010-04-11 09:27:54 +08001759 bd->props.brightness = read_brightness(bd);
Corentin Charyb7187262011-02-06 13:28:39 +01001760 bd->props.power = power;
Yong Wang3d7b1652010-04-11 09:27:54 +08001761 backlight_update_status(bd);
1762
AceLan Kaoc87992d2012-03-20 09:53:08 +01001763 asus->driver->brightness = bd->props.brightness;
1764
Yong Wang3d7b1652010-04-11 09:27:54 +08001765 return 0;
1766}
1767
Corentin Charye12e6d92011-02-26 10:20:31 +01001768static void asus_wmi_backlight_exit(struct asus_wmi *asus)
Yong Wang3d7b1652010-04-11 09:27:54 +08001769{
Markus Elfring00981812014-11-24 20:30:29 +01001770 backlight_device_unregister(asus->backlight_device);
Yong Wang3d7b1652010-04-11 09:27:54 +08001771
Corentin Charye12e6d92011-02-26 10:20:31 +01001772 asus->backlight_device = NULL;
Yong Wang3d7b1652010-04-11 09:27:54 +08001773}
1774
AceLan Kaoa2a96f02012-10-03 11:26:31 +02001775static int is_display_toggle(int code)
1776{
1777 /* display toggle keys */
1778 if ((code >= 0x61 && code <= 0x67) ||
1779 (code >= 0x8c && code <= 0x93) ||
1780 (code >= 0xa0 && code <= 0xa7) ||
1781 (code >= 0xd0 && code <= 0xd5))
1782 return 1;
1783
1784 return 0;
1785}
1786
Yurii Pavlovskyi54a31212019-05-14 21:04:48 +02001787/* Fn-lock ********************************************************************/
1788
Chris Chiu487579b2019-04-18 14:46:48 +08001789static bool asus_wmi_has_fnlock_key(struct asus_wmi *asus)
1790{
1791 u32 result;
1792
1793 asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_FNLOCK, &result);
1794
1795 return (result & ASUS_WMI_DSTS_PRESENCE_BIT) &&
1796 !(result & ASUS_WMI_FNLOCK_BIOS_DISABLED);
1797}
1798
1799static void asus_wmi_fnlock_update(struct asus_wmi *asus)
1800{
1801 int mode = asus->fnlock_locked;
1802
1803 asus_wmi_set_devstate(ASUS_WMI_DEVID_FNLOCK, mode, NULL);
1804}
1805
Yurii Pavlovskyi54a31212019-05-14 21:04:48 +02001806/* WMI events *****************************************************************/
1807
Yurii Pavlovskyi8abd752b2019-05-14 21:01:24 +02001808static int asus_wmi_get_event_code(u32 value)
Yong Wang3d7b1652010-04-11 09:27:54 +08001809{
Yong Wang3d7b1652010-04-11 09:27:54 +08001810 struct acpi_buffer response = { ACPI_ALLOCATE_BUFFER, NULL };
1811 union acpi_object *obj;
1812 acpi_status status;
1813 int code;
Yong Wang3d7b1652010-04-11 09:27:54 +08001814
1815 status = wmi_get_event_data(value, &response);
Yurii Pavlovskyi8abd752b2019-05-14 21:01:24 +02001816 if (ACPI_FAILURE(status)) {
1817 pr_warn("Failed to get WMI notify code: %s\n",
1818 acpi_format_exception(status));
1819 return -EIO;
Yong Wang3d7b1652010-04-11 09:27:54 +08001820 }
1821
1822 obj = (union acpi_object *)response.pointer;
1823
Yurii Pavlovskyi8abd752b2019-05-14 21:01:24 +02001824 if (obj && obj->type == ACPI_TYPE_INTEGER)
1825 code = (int)(obj->integer.value & WMI_EVENT_MASK);
1826 else
1827 code = -EIO;
Yong Wang3d7b1652010-04-11 09:27:54 +08001828
Yurii Pavlovskyi8abd752b2019-05-14 21:01:24 +02001829 kfree(obj);
1830 return code;
1831}
1832
1833static void asus_wmi_handle_event_code(int code, struct asus_wmi *asus)
1834{
1835 int orig_code;
1836 unsigned int key_value = 1;
1837 bool autorelease = 1;
1838
Corentin Chary57ab7da2011-02-26 10:20:32 +01001839 orig_code = code;
Yong Wang3d7b1652010-04-11 09:27:54 +08001840
Seth Forsheec4453f62011-07-01 11:34:27 +02001841 if (asus->driver->key_filter) {
1842 asus->driver->key_filter(asus->driver, &code, &key_value,
1843 &autorelease);
1844 if (code == ASUS_WMI_KEY_IGNORE)
Yurii Pavlovskyi8abd752b2019-05-14 21:01:24 +02001845 return;
Seth Forsheec4453f62011-07-01 11:34:27 +02001846 }
1847
Corentin Chary57ab7da2011-02-26 10:20:32 +01001848 if (code >= NOTIFY_BRNUP_MIN && code <= NOTIFY_BRNUP_MAX)
Corentin Chary3ba03022012-11-29 09:12:38 +01001849 code = ASUS_WMI_BRN_UP;
Yurii Pavlovskyi8abd752b2019-05-14 21:01:24 +02001850 else if (code >= NOTIFY_BRNDOWN_MIN && code <= NOTIFY_BRNDOWN_MAX)
Corentin Chary3ba03022012-11-29 09:12:38 +01001851 code = ASUS_WMI_BRN_DOWN;
Yong Wang3d7b1652010-04-11 09:27:54 +08001852
Corentin Chary3ba03022012-11-29 09:12:38 +01001853 if (code == ASUS_WMI_BRN_DOWN || code == ASUS_WMI_BRN_UP) {
Hans de Goede62c4aa12015-06-16 16:27:58 +02001854 if (acpi_video_get_backlight_type() == acpi_backlight_vendor) {
Corentin Chary57ab7da2011-02-26 10:20:32 +01001855 asus_wmi_backlight_notify(asus, orig_code);
Yurii Pavlovskyi8abd752b2019-05-14 21:01:24 +02001856 return;
AceLan Kaoa2a96f02012-10-03 11:26:31 +02001857 }
AceLan Kaoa2a96f02012-10-03 11:26:31 +02001858 }
1859
Chris Chiudbb3d782018-06-20 22:46:44 +08001860 if (code == NOTIFY_KBD_BRTUP) {
Jian-Hong Pan29f6eb52018-10-22 18:00:04 +08001861 kbd_led_set_by_kbd(asus, asus->kbd_led_wk + 1);
Yurii Pavlovskyi8abd752b2019-05-14 21:01:24 +02001862 return;
Chris Chiudbb3d782018-06-20 22:46:44 +08001863 }
1864 if (code == NOTIFY_KBD_BRTDWN) {
Jian-Hong Pan29f6eb52018-10-22 18:00:04 +08001865 kbd_led_set_by_kbd(asus, asus->kbd_led_wk - 1);
Yurii Pavlovskyi8abd752b2019-05-14 21:01:24 +02001866 return;
Chris Chiudbb3d782018-06-20 22:46:44 +08001867 }
Chris Chiued99d292018-06-20 22:46:45 +08001868 if (code == NOTIFY_KBD_BRTTOGGLE) {
1869 if (asus->kbd_led_wk == asus->kbd_led.max_brightness)
Jian-Hong Pan29f6eb52018-10-22 18:00:04 +08001870 kbd_led_set_by_kbd(asus, 0);
Chris Chiued99d292018-06-20 22:46:45 +08001871 else
Jian-Hong Pan29f6eb52018-10-22 18:00:04 +08001872 kbd_led_set_by_kbd(asus, asus->kbd_led_wk + 1);
Yurii Pavlovskyi8abd752b2019-05-14 21:01:24 +02001873 return;
Chris Chiued99d292018-06-20 22:46:45 +08001874 }
Chris Chiudbb3d782018-06-20 22:46:44 +08001875
Chris Chiu487579b2019-04-18 14:46:48 +08001876 if (code == NOTIFY_FNLOCK_TOGGLE) {
1877 asus->fnlock_locked = !asus->fnlock_locked;
1878 asus_wmi_fnlock_update(asus);
Yurii Pavlovskyi8abd752b2019-05-14 21:01:24 +02001879 return;
Chris Chiu487579b2019-04-18 14:46:48 +08001880 }
1881
Daniel Drake9af93db2019-07-17 13:10:58 +08001882 if (asus->fan_boost_mode_available && code == NOTIFY_KBD_FBM) {
1883 fan_boost_mode_switch_next(asus);
Yurii Pavlovskyib096f622019-05-14 21:07:05 +02001884 return;
1885 }
1886
Yurii Pavlovskyi8abd752b2019-05-14 21:01:24 +02001887 if (is_display_toggle(code) && asus->driver->quirks->no_display_toggle)
1888 return;
AceLan Kaoa2a96f02012-10-03 11:26:31 +02001889
1890 if (!sparse_keymap_report_event(asus->inputdev, code,
1891 key_value, autorelease))
Corentin Chary57ab7da2011-02-26 10:20:32 +01001892 pr_info("Unknown key %x pressed\n", code);
Yong Wang3d7b1652010-04-11 09:27:54 +08001893}
1894
Yurii Pavlovskyi8abd752b2019-05-14 21:01:24 +02001895static void asus_wmi_notify(u32 value, void *context)
1896{
1897 struct asus_wmi *asus = context;
Yurii Pavlovskyi1a373d12019-05-14 21:02:09 +02001898 int code;
1899 int i;
Yurii Pavlovskyi8abd752b2019-05-14 21:01:24 +02001900
Yurii Pavlovskyi1a373d12019-05-14 21:02:09 +02001901 for (i = 0; i < WMI_EVENT_QUEUE_SIZE + 1; i++) {
1902 code = asus_wmi_get_event_code(value);
1903
1904 if (code < 0) {
1905 pr_warn("Failed to get notify code: %d\n", code);
1906 return;
1907 }
1908
1909 if (code == WMI_EVENT_QUEUE_END || code == WMI_EVENT_MASK)
1910 return;
1911
1912 asus_wmi_handle_event_code(code, asus);
1913
1914 /*
1915 * Double check that queue is present:
1916 * ATK (with queue) uses 0xff, ASUSWMI (without) 0xd2.
1917 */
1918 if (!asus->wmi_event_queue || value != WMI_EVENT_VALUE_ATK)
1919 return;
Yurii Pavlovskyi8abd752b2019-05-14 21:01:24 +02001920 }
1921
Yurii Pavlovskyi1a373d12019-05-14 21:02:09 +02001922 pr_warn("Failed to process event queue, last code: 0x%x\n", code);
1923}
1924
1925static int asus_wmi_notify_queue_flush(struct asus_wmi *asus)
1926{
1927 int code;
1928 int i;
1929
1930 for (i = 0; i < WMI_EVENT_QUEUE_SIZE + 1; i++) {
1931 code = asus_wmi_get_event_code(WMI_EVENT_VALUE_ATK);
1932
1933 if (code < 0) {
1934 pr_warn("Failed to get event during flush: %d\n", code);
1935 return code;
1936 }
1937
1938 if (code == WMI_EVENT_QUEUE_END || code == WMI_EVENT_MASK)
1939 return 0;
1940 }
1941
1942 pr_warn("Failed to flush event queue\n");
1943 return -EIO;
Yong Wangee027e42010-03-21 10:26:34 +08001944}
1945
Yurii Pavlovskyi54a31212019-05-14 21:04:48 +02001946/* Sysfs **********************************************************************/
1947
Corentin Chary9e1565b2011-02-06 13:28:36 +01001948static int parse_arg(const char *buf, unsigned long count, int *val)
1949{
1950 if (!count)
1951 return 0;
1952 if (sscanf(buf, "%i", val) != 1)
1953 return -EINVAL;
1954 return count;
1955}
1956
Corentin Chary1d070f82011-02-26 10:20:36 +01001957static ssize_t store_sys_wmi(struct asus_wmi *asus, int devid,
1958 const char *buf, size_t count)
Corentin Chary9e1565b2011-02-06 13:28:36 +01001959{
Corentin Chary9e1565b2011-02-06 13:28:36 +01001960 u32 retval;
Corentin Charyd33da3b2011-02-26 10:20:35 +01001961 int rv, err, value;
Corentin Chary9e1565b2011-02-06 13:28:36 +01001962
Corentin Chary1d070f82011-02-26 10:20:36 +01001963 value = asus_wmi_get_devstate_simple(asus, devid);
Dan Carpenterb8298342015-11-11 01:18:16 +03001964 if (value < 0)
Corentin Chary9e1565b2011-02-06 13:28:36 +01001965 return value;
1966
1967 rv = parse_arg(buf, count, &value);
Corentin Charyd33da3b2011-02-26 10:20:35 +01001968 err = asus_wmi_set_devstate(devid, value, &retval);
Corentin Chary9e1565b2011-02-06 13:28:36 +01001969
Corentin Charyd33da3b2011-02-26 10:20:35 +01001970 if (err < 0)
1971 return err;
1972
Corentin Chary9e1565b2011-02-06 13:28:36 +01001973 return rv;
1974}
1975
Corentin Chary1d070f82011-02-26 10:20:36 +01001976static ssize_t show_sys_wmi(struct asus_wmi *asus, int devid, char *buf)
Corentin Chary9e1565b2011-02-06 13:28:36 +01001977{
Corentin Chary1d070f82011-02-26 10:20:36 +01001978 int value = asus_wmi_get_devstate_simple(asus, devid);
Corentin Chary9e1565b2011-02-06 13:28:36 +01001979
1980 if (value < 0)
1981 return value;
1982
1983 return sprintf(buf, "%d\n", value);
1984}
1985
Corentin Charye12e6d92011-02-26 10:20:31 +01001986#define ASUS_WMI_CREATE_DEVICE_ATTR(_name, _mode, _cm) \
Corentin Chary9e1565b2011-02-06 13:28:36 +01001987 static ssize_t show_##_name(struct device *dev, \
1988 struct device_attribute *attr, \
1989 char *buf) \
1990 { \
Corentin Chary1d070f82011-02-26 10:20:36 +01001991 struct asus_wmi *asus = dev_get_drvdata(dev); \
1992 \
1993 return show_sys_wmi(asus, _cm, buf); \
Corentin Chary9e1565b2011-02-06 13:28:36 +01001994 } \
1995 static ssize_t store_##_name(struct device *dev, \
1996 struct device_attribute *attr, \
1997 const char *buf, size_t count) \
1998 { \
Corentin Chary1d070f82011-02-26 10:20:36 +01001999 struct asus_wmi *asus = dev_get_drvdata(dev); \
2000 \
2001 return store_sys_wmi(asus, _cm, buf, count); \
Corentin Chary9e1565b2011-02-06 13:28:36 +01002002 } \
2003 static struct device_attribute dev_attr_##_name = { \
2004 .attr = { \
2005 .name = __stringify(_name), \
2006 .mode = _mode }, \
2007 .show = show_##_name, \
2008 .store = store_##_name, \
2009 }
2010
Corentin Charye12e6d92011-02-26 10:20:31 +01002011ASUS_WMI_CREATE_DEVICE_ATTR(touchpad, 0644, ASUS_WMI_DEVID_TOUCHPAD);
2012ASUS_WMI_CREATE_DEVICE_ATTR(camera, 0644, ASUS_WMI_DEVID_CAMERA);
2013ASUS_WMI_CREATE_DEVICE_ATTR(cardr, 0644, ASUS_WMI_DEVID_CARDREADER);
AceLan Kaoc0b91b62012-06-13 09:32:07 +02002014ASUS_WMI_CREATE_DEVICE_ATTR(lid_resume, 0644, ASUS_WMI_DEVID_LID_RESUME);
Oleksij Rempelaca234f2016-04-01 13:35:21 +02002015ASUS_WMI_CREATE_DEVICE_ATTR(als_enable, 0644, ASUS_WMI_DEVID_ALS_ENABLE);
Corentin Chary9e1565b2011-02-06 13:28:36 +01002016
Jérémy Lefaurea8fe3422017-04-21 22:19:45 -04002017static ssize_t cpufv_store(struct device *dev, struct device_attribute *attr,
Dmitry Torokhov67fa38e2010-11-03 11:14:01 -07002018 const char *buf, size_t count)
Chris Bagwell7f80d732010-10-11 18:47:18 -05002019{
Corentin Chary3df5fda2011-07-01 11:34:38 +02002020 int value, rv;
Chris Bagwell7f80d732010-10-11 18:47:18 -05002021
2022 if (!count || sscanf(buf, "%i", &value) != 1)
2023 return -EINVAL;
2024 if (value < 0 || value > 2)
2025 return -EINVAL;
2026
Corentin Chary3df5fda2011-07-01 11:34:38 +02002027 rv = asus_wmi_evaluate_method(ASUS_WMI_METHODID_CFVS, value, 0, NULL);
2028 if (rv < 0)
2029 return rv;
2030
2031 return count;
Chris Bagwell7f80d732010-10-11 18:47:18 -05002032}
2033
Jérémy Lefaurea8fe3422017-04-21 22:19:45 -04002034static DEVICE_ATTR_WO(cpufv);
Chris Bagwell7f80d732010-10-11 18:47:18 -05002035
Corentin Chary4e37b422010-11-29 08:14:08 +01002036static struct attribute *platform_attributes[] = {
2037 &dev_attr_cpufv.attr,
Corentin Chary9e1565b2011-02-06 13:28:36 +01002038 &dev_attr_camera.attr,
2039 &dev_attr_cardr.attr,
Corentin Chary4615bb62011-02-06 13:28:42 +01002040 &dev_attr_touchpad.attr,
AceLan Kaoc0b91b62012-06-13 09:32:07 +02002041 &dev_attr_lid_resume.attr,
Oleksij Rempelaca234f2016-04-01 13:35:21 +02002042 &dev_attr_als_enable.attr,
Daniel Drake9af93db2019-07-17 13:10:58 +08002043 &dev_attr_fan_boost_mode.attr,
Corentin Chary4e37b422010-11-29 08:14:08 +01002044 NULL
2045};
2046
Al Viro587a1f12011-07-23 23:11:19 -04002047static umode_t asus_sysfs_is_visible(struct kobject *kobj,
Corentin Charye12e6d92011-02-26 10:20:31 +01002048 struct attribute *attr, int idx)
Corentin Chary9e1565b2011-02-06 13:28:36 +01002049{
Corentin Chary1d070f82011-02-26 10:20:36 +01002050 struct device *dev = container_of(kobj, struct device, kobj);
Wolfram Sangd605ca22018-04-19 16:06:10 +02002051 struct asus_wmi *asus = dev_get_drvdata(dev);
Corentin Chary1d070f82011-02-26 10:20:36 +01002052 bool ok = true;
Corentin Chary9e1565b2011-02-06 13:28:36 +01002053 int devid = -1;
2054
2055 if (attr == &dev_attr_camera.attr)
Corentin Charye12e6d92011-02-26 10:20:31 +01002056 devid = ASUS_WMI_DEVID_CAMERA;
Corentin Chary9e1565b2011-02-06 13:28:36 +01002057 else if (attr == &dev_attr_cardr.attr)
Corentin Charye12e6d92011-02-26 10:20:31 +01002058 devid = ASUS_WMI_DEVID_CARDREADER;
Corentin Chary4615bb62011-02-06 13:28:42 +01002059 else if (attr == &dev_attr_touchpad.attr)
Corentin Charye12e6d92011-02-26 10:20:31 +01002060 devid = ASUS_WMI_DEVID_TOUCHPAD;
AceLan Kaoc0b91b62012-06-13 09:32:07 +02002061 else if (attr == &dev_attr_lid_resume.attr)
2062 devid = ASUS_WMI_DEVID_LID_RESUME;
Oleksij Rempelaca234f2016-04-01 13:35:21 +02002063 else if (attr == &dev_attr_als_enable.attr)
2064 devid = ASUS_WMI_DEVID_ALS_ENABLE;
Daniel Drake9af93db2019-07-17 13:10:58 +08002065 else if (attr == &dev_attr_fan_boost_mode.attr)
2066 ok = asus->fan_boost_mode_available;
Corentin Chary9e1565b2011-02-06 13:28:36 +01002067
2068 if (devid != -1)
Corentin Chary1d070f82011-02-26 10:20:36 +01002069 ok = !(asus_wmi_get_devstate_simple(asus, devid) < 0);
Corentin Chary9e1565b2011-02-06 13:28:36 +01002070
Corentin Chary1d070f82011-02-26 10:20:36 +01002071 return ok ? attr->mode : 0;
Corentin Chary9e1565b2011-02-06 13:28:36 +01002072}
2073
Arvind Yadave90d9ba2017-07-11 16:18:19 +05302074static const struct attribute_group platform_attribute_group = {
Corentin Charye12e6d92011-02-26 10:20:31 +01002075 .is_visible = asus_sysfs_is_visible,
2076 .attrs = platform_attributes
Corentin Chary4e37b422010-11-29 08:14:08 +01002077};
2078
Corentin Charye12e6d92011-02-26 10:20:31 +01002079static void asus_wmi_sysfs_exit(struct platform_device *device)
Chris Bagwell7f80d732010-10-11 18:47:18 -05002080{
Corentin Chary4e37b422010-11-29 08:14:08 +01002081 sysfs_remove_group(&device->dev.kobj, &platform_attribute_group);
Chris Bagwell7f80d732010-10-11 18:47:18 -05002082}
2083
Corentin Charye12e6d92011-02-26 10:20:31 +01002084static int asus_wmi_sysfs_init(struct platform_device *device)
Chris Bagwell7f80d732010-10-11 18:47:18 -05002085{
Corentin Chary4e37b422010-11-29 08:14:08 +01002086 return sysfs_create_group(&device->dev.kobj, &platform_attribute_group);
Chris Bagwell7f80d732010-10-11 18:47:18 -05002087}
2088
Yurii Pavlovskyi54a31212019-05-14 21:04:48 +02002089/* Platform device ************************************************************/
2090
Joe Perches39ddf3b2011-03-29 15:21:32 -07002091static int asus_wmi_platform_init(struct asus_wmi *asus)
Corentin Chary27c136c2010-11-29 08:14:05 +01002092{
Yurii Pavlovskyie0668f22019-05-14 21:00:31 +02002093 struct device *dev = &asus->platform_device->dev;
2094 char *wmi_uid;
Corentin Chary46dbca82011-02-26 10:20:38 +01002095 int rv;
2096
2097 /* INIT enable hotkeys on some models */
2098 if (!asus_wmi_evaluate_method(ASUS_WMI_METHODID_INIT, 0, 0, &rv))
vic0ed60652013-05-22 21:32:10 +03002099 pr_info("Initialization: %#x\n", rv);
Corentin Chary46dbca82011-02-26 10:20:38 +01002100
2101 /* We don't know yet what to do with this version... */
2102 if (!asus_wmi_evaluate_method(ASUS_WMI_METHODID_SPEC, 0, 0x9, &rv)) {
vic0ed60652013-05-22 21:32:10 +03002103 pr_info("BIOS WMI version: %d.%d\n", rv >> 16, rv & 0xFF);
Corentin Chary46dbca82011-02-26 10:20:38 +01002104 asus->spec = rv;
2105 }
2106
2107 /*
2108 * The SFUN method probably allows the original driver to get the list
2109 * of features supported by a given model. For now, 0x0100 or 0x0800
2110 * bit signifies that the laptop is equipped with a Wi-Fi MiniPCI card.
2111 * The significance of others is yet to be found.
2112 */
2113 if (!asus_wmi_evaluate_method(ASUS_WMI_METHODID_SFUN, 0, 0, &rv)) {
vic0ed60652013-05-22 21:32:10 +03002114 pr_info("SFUN value: %#x\n", rv);
Corentin Chary46dbca82011-02-26 10:20:38 +01002115 asus->sfun = rv;
2116 }
2117
Corentin Chary1d070f82011-02-26 10:20:36 +01002118 /*
2119 * Eee PC and Notebooks seems to have different method_id for DSTS,
2120 * but it may also be related to the BIOS's SPEC.
2121 * Note, on most Eeepc, there is no way to check if a method exist
2122 * or note, while on notebooks, they returns 0xFFFFFFFE on failure,
2123 * but once again, SPEC may probably be used for that kind of things.
Yurii Pavlovskyie0668f22019-05-14 21:00:31 +02002124 *
2125 * Additionally at least TUF Gaming series laptops return nothing for
2126 * unknown methods, so the detection in this way is not possible.
2127 *
2128 * There is strong indication that only ACPI WMI devices that have _UID
2129 * equal to "ASUSWMI" use DCTS whereas those with "ATK" use DSTS.
Corentin Chary1d070f82011-02-26 10:20:36 +01002130 */
Yurii Pavlovskyie0668f22019-05-14 21:00:31 +02002131 wmi_uid = wmi_get_acpi_device_uid(ASUS_WMI_MGMT_GUID);
2132 if (!wmi_uid)
2133 return -ENODEV;
2134
2135 if (!strcmp(wmi_uid, ASUS_ACPI_UID_ASUSWMI)) {
2136 dev_info(dev, "Detected ASUSWMI, use DCTS\n");
2137 asus->dsts_id = ASUS_WMI_METHODID_DCTS;
2138 } else {
2139 dev_info(dev, "Detected %s, not ASUSWMI, use DSTS\n", wmi_uid);
Corentin Chary1d070f82011-02-26 10:20:36 +01002140 asus->dsts_id = ASUS_WMI_METHODID_DSTS;
Yurii Pavlovskyie0668f22019-05-14 21:00:31 +02002141 }
Corentin Chary1d070f82011-02-26 10:20:36 +01002142
Yurii Pavlovskyi1a373d12019-05-14 21:02:09 +02002143 /*
2144 * Some devices can have multiple event codes stored in a queue before
2145 * the module load if it was unloaded intermittently after calling
2146 * the INIT method (enables event handling). The WMI notify handler is
2147 * expected to retrieve all event codes until a retrieved code equals
2148 * queue end marker (One or Ones). Old codes are flushed from the queue
2149 * upon module load. Not enabling this when it should be has minimal
2150 * visible impact so fall back if anything goes wrong.
2151 */
2152 wmi_uid = wmi_get_acpi_device_uid(asus->driver->event_guid);
2153 if (wmi_uid && !strcmp(wmi_uid, ASUS_ACPI_UID_ATK)) {
2154 dev_info(dev, "Detected ATK, enable event queue\n");
2155
2156 if (!asus_wmi_notify_queue_flush(asus))
2157 asus->wmi_event_queue = true;
2158 }
Corentin Chary1d070f82011-02-26 10:20:36 +01002159
Corentin Charyfddbfed2011-07-01 11:34:39 +02002160 /* CWAP allow to define the behavior of the Fn+F2 key,
2161 * this method doesn't seems to be present on Eee PCs */
Corentin Chary6a2bccc2012-03-20 09:53:10 +01002162 if (asus->driver->quirks->wapf >= 0)
Corentin Charyfddbfed2011-07-01 11:34:39 +02002163 asus_wmi_set_devstate(ASUS_WMI_DEVID_CWAP,
Corentin Chary6a2bccc2012-03-20 09:53:10 +01002164 asus->driver->quirks->wapf, NULL);
Corentin Charyfddbfed2011-07-01 11:34:39 +02002165
Yurii Pavlovskyib096f622019-05-14 21:07:05 +02002166 return 0;
Corentin Chary27c136c2010-11-29 08:14:05 +01002167}
2168
Yurii Pavlovskyi54a31212019-05-14 21:04:48 +02002169/* debugfs ********************************************************************/
Corentin Chary27c136c2010-11-29 08:14:05 +01002170
Corentin Charye12e6d92011-02-26 10:20:31 +01002171struct asus_wmi_debugfs_node {
2172 struct asus_wmi *asus;
Corentin Chary8c1b2d82010-11-29 08:14:09 +01002173 char *name;
Corentin Charye12e6d92011-02-26 10:20:31 +01002174 int (*show) (struct seq_file *m, void *data);
Corentin Chary8c1b2d82010-11-29 08:14:09 +01002175};
2176
2177static int show_dsts(struct seq_file *m, void *data)
2178{
Corentin Charye12e6d92011-02-26 10:20:31 +01002179 struct asus_wmi *asus = m->private;
Corentin Charyd33da3b2011-02-26 10:20:35 +01002180 int err;
Corentin Chary8c1b2d82010-11-29 08:14:09 +01002181 u32 retval = -1;
2182
Corentin Chary1d070f82011-02-26 10:20:36 +01002183 err = asus_wmi_get_devstate(asus, asus->debug.dev_id, &retval);
Corentin Chary8c1b2d82010-11-29 08:14:09 +01002184
Corentin Charyd33da3b2011-02-26 10:20:35 +01002185 if (err < 0)
2186 return err;
Corentin Chary8c1b2d82010-11-29 08:14:09 +01002187
Corentin Charyef343492011-02-26 10:20:39 +01002188 seq_printf(m, "DSTS(%#x) = %#x\n", asus->debug.dev_id, retval);
Corentin Chary8c1b2d82010-11-29 08:14:09 +01002189
2190 return 0;
2191}
2192
2193static int show_devs(struct seq_file *m, void *data)
2194{
Corentin Charye12e6d92011-02-26 10:20:31 +01002195 struct asus_wmi *asus = m->private;
Corentin Charyd33da3b2011-02-26 10:20:35 +01002196 int err;
Corentin Chary8c1b2d82010-11-29 08:14:09 +01002197 u32 retval = -1;
2198
Corentin Charyd33da3b2011-02-26 10:20:35 +01002199 err = asus_wmi_set_devstate(asus->debug.dev_id, asus->debug.ctrl_param,
2200 &retval);
2201
2202 if (err < 0)
2203 return err;
Corentin Chary8c1b2d82010-11-29 08:14:09 +01002204
Corentin Charyef343492011-02-26 10:20:39 +01002205 seq_printf(m, "DEVS(%#x, %#x) = %#x\n", asus->debug.dev_id,
Corentin Charye12e6d92011-02-26 10:20:31 +01002206 asus->debug.ctrl_param, retval);
Corentin Chary8c1b2d82010-11-29 08:14:09 +01002207
2208 return 0;
2209}
2210
Corentin Charyef343492011-02-26 10:20:39 +01002211static int show_call(struct seq_file *m, void *data)
2212{
2213 struct asus_wmi *asus = m->private;
2214 struct bios_args args = {
2215 .arg0 = asus->debug.dev_id,
2216 .arg1 = asus->debug.ctrl_param,
2217 };
2218 struct acpi_buffer input = { (acpi_size) sizeof(args), &args };
2219 struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER, NULL };
2220 union acpi_object *obj;
2221 acpi_status status;
2222
2223 status = wmi_evaluate_method(ASUS_WMI_MGMT_GUID,
Pali Rohár0fe57262017-08-12 09:44:16 +02002224 0, asus->debug.method_id,
Corentin Charyef343492011-02-26 10:20:39 +01002225 &input, &output);
2226
2227 if (ACPI_FAILURE(status))
2228 return -EIO;
2229
2230 obj = (union acpi_object *)output.pointer;
2231 if (obj && obj->type == ACPI_TYPE_INTEGER)
2232 seq_printf(m, "%#x(%#x, %#x) = %#x\n", asus->debug.method_id,
2233 asus->debug.dev_id, asus->debug.ctrl_param,
2234 (u32) obj->integer.value);
2235 else
2236 seq_printf(m, "%#x(%#x, %#x) = t:%d\n", asus->debug.method_id,
2237 asus->debug.dev_id, asus->debug.ctrl_param,
Dan Carpentera1d60862011-03-15 10:07:37 +03002238 obj ? obj->type : -1);
Corentin Charyef343492011-02-26 10:20:39 +01002239
2240 kfree(obj);
2241
2242 return 0;
2243}
2244
Corentin Charye12e6d92011-02-26 10:20:31 +01002245static struct asus_wmi_debugfs_node asus_wmi_debug_files[] = {
2246 {NULL, "devs", show_devs},
2247 {NULL, "dsts", show_dsts},
Corentin Charyef343492011-02-26 10:20:39 +01002248 {NULL, "call", show_call},
Corentin Chary8c1b2d82010-11-29 08:14:09 +01002249};
2250
Corentin Charye12e6d92011-02-26 10:20:31 +01002251static int asus_wmi_debugfs_open(struct inode *inode, struct file *file)
Corentin Chary8c1b2d82010-11-29 08:14:09 +01002252{
Corentin Charye12e6d92011-02-26 10:20:31 +01002253 struct asus_wmi_debugfs_node *node = inode->i_private;
Corentin Chary8c1b2d82010-11-29 08:14:09 +01002254
Corentin Charye12e6d92011-02-26 10:20:31 +01002255 return single_open(file, node->show, node->asus);
Corentin Chary8c1b2d82010-11-29 08:14:09 +01002256}
2257
Corentin Charye12e6d92011-02-26 10:20:31 +01002258static const struct file_operations asus_wmi_debugfs_io_ops = {
Corentin Chary8c1b2d82010-11-29 08:14:09 +01002259 .owner = THIS_MODULE,
Corentin Charye12e6d92011-02-26 10:20:31 +01002260 .open = asus_wmi_debugfs_open,
Corentin Chary8c1b2d82010-11-29 08:14:09 +01002261 .read = seq_read,
2262 .llseek = seq_lseek,
2263 .release = single_release,
2264};
2265
Corentin Charye12e6d92011-02-26 10:20:31 +01002266static void asus_wmi_debugfs_exit(struct asus_wmi *asus)
Corentin Chary8c1b2d82010-11-29 08:14:09 +01002267{
Corentin Charye12e6d92011-02-26 10:20:31 +01002268 debugfs_remove_recursive(asus->debug.root);
Corentin Chary8c1b2d82010-11-29 08:14:09 +01002269}
2270
Greg Kroah-Hartmand2785d32019-06-12 14:12:52 +02002271static void asus_wmi_debugfs_init(struct asus_wmi *asus)
Corentin Chary8c1b2d82010-11-29 08:14:09 +01002272{
Corentin Chary8c1b2d82010-11-29 08:14:09 +01002273 int i;
2274
Corentin Charye12e6d92011-02-26 10:20:31 +01002275 asus->debug.root = debugfs_create_dir(asus->driver->name, NULL);
Corentin Chary8c1b2d82010-11-29 08:14:09 +01002276
Greg Kroah-Hartmand2785d32019-06-12 14:12:52 +02002277 debugfs_create_x32("method_id", S_IRUGO | S_IWUSR, asus->debug.root,
2278 &asus->debug.method_id);
Corentin Charyef343492011-02-26 10:20:39 +01002279
Greg Kroah-Hartmand2785d32019-06-12 14:12:52 +02002280 debugfs_create_x32("dev_id", S_IRUGO | S_IWUSR, asus->debug.root,
2281 &asus->debug.dev_id);
Corentin Chary8c1b2d82010-11-29 08:14:09 +01002282
Greg Kroah-Hartmand2785d32019-06-12 14:12:52 +02002283 debugfs_create_x32("ctrl_param", S_IRUGO | S_IWUSR, asus->debug.root,
2284 &asus->debug.ctrl_param);
Corentin Chary8c1b2d82010-11-29 08:14:09 +01002285
Corentin Charye12e6d92011-02-26 10:20:31 +01002286 for (i = 0; i < ARRAY_SIZE(asus_wmi_debug_files); i++) {
2287 struct asus_wmi_debugfs_node *node = &asus_wmi_debug_files[i];
Corentin Chary8c1b2d82010-11-29 08:14:09 +01002288
Corentin Charye12e6d92011-02-26 10:20:31 +01002289 node->asus = asus;
Greg Kroah-Hartmand2785d32019-06-12 14:12:52 +02002290 debugfs_create_file(node->name, S_IFREG | S_IRUGO,
2291 asus->debug.root, node,
2292 &asus_wmi_debugfs_io_ops);
Corentin Chary8c1b2d82010-11-29 08:14:09 +01002293 }
Corentin Chary8c1b2d82010-11-29 08:14:09 +01002294}
2295
Yurii Pavlovskyi54a31212019-05-14 21:04:48 +02002296/* Init / exit ****************************************************************/
Kast Bernd53e755c2015-05-13 16:24:16 +02002297
Corentin Charye12e6d92011-02-26 10:20:31 +01002298static int asus_wmi_add(struct platform_device *pdev)
Corentin Charyafa7c882011-02-06 13:28:28 +01002299{
Corentin Charye12e6d92011-02-26 10:20:31 +01002300 struct platform_driver *pdrv = to_platform_driver(pdev->dev.driver);
2301 struct asus_wmi_driver *wdrv = to_asus_wmi_driver(pdrv);
2302 struct asus_wmi *asus;
Hans de Goeded8c66f62014-07-08 10:47:21 +02002303 const char *chassis_type;
Yong Wangee027e42010-03-21 10:26:34 +08002304 acpi_status status;
Corentin Chary27c136c2010-11-29 08:14:05 +01002305 int err;
AceLan Kaoa50bd122012-07-26 17:13:31 +08002306 u32 result;
Yong Wangee027e42010-03-21 10:26:34 +08002307
Corentin Charye12e6d92011-02-26 10:20:31 +01002308 asus = kzalloc(sizeof(struct asus_wmi), GFP_KERNEL);
2309 if (!asus)
Corentin Charya04ce292011-02-06 13:28:33 +01002310 return -ENOMEM;
2311
Corentin Charye12e6d92011-02-26 10:20:31 +01002312 asus->driver = wdrv;
2313 asus->platform_device = pdev;
2314 wdrv->platform_device = pdev;
2315 platform_set_drvdata(asus->platform_device, asus);
Corentin Chary27c136c2010-11-29 08:14:05 +01002316
AceLan Kaoc87992d2012-03-20 09:53:08 +01002317 if (wdrv->detect_quirks)
2318 wdrv->detect_quirks(asus->driver);
Corentin Charyafa7c882011-02-06 13:28:28 +01002319
Corentin Charye12e6d92011-02-26 10:20:31 +01002320 err = asus_wmi_platform_init(asus);
Corentin Chary27c136c2010-11-29 08:14:05 +01002321 if (err)
2322 goto fail_platform;
Yong Wang45f2c692010-04-11 09:27:19 +08002323
Daniel Drake9af93db2019-07-17 13:10:58 +08002324 err = fan_boost_mode_check_present(asus);
Yurii Pavlovskyib096f622019-05-14 21:07:05 +02002325 if (err)
Daniel Drake9af93db2019-07-17 13:10:58 +08002326 goto fail_fan_boost_mode;
Yurii Pavlovskyib096f622019-05-14 21:07:05 +02002327
2328 err = asus_wmi_sysfs_init(asus->platform_device);
2329 if (err)
2330 goto fail_sysfs;
2331
Corentin Charye12e6d92011-02-26 10:20:31 +01002332 err = asus_wmi_input_init(asus);
Yong Wang45f2c692010-04-11 09:27:19 +08002333 if (err)
Corentin Chary27c136c2010-11-29 08:14:05 +01002334 goto fail_input;
Yong Wang3d7b1652010-04-11 09:27:54 +08002335
Kast Bernd53e755c2015-05-13 16:24:16 +02002336 err = asus_wmi_fan_init(asus); /* probably no problems on error */
2337 asus_hwmon_fan_set_auto(asus);
2338
Corentin Charye07babd2011-02-26 10:20:42 +01002339 err = asus_wmi_hwmon_init(asus);
2340 if (err)
2341 goto fail_hwmon;
2342
Corentin Charye12e6d92011-02-26 10:20:31 +01002343 err = asus_wmi_led_init(asus);
Corentin Chary084fca62010-11-29 08:14:06 +01002344 if (err)
2345 goto fail_leds;
2346
João Paulo Rechi Vita71050ae2017-02-20 14:50:22 -05002347 asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_WLAN, &result);
2348 if (result & (ASUS_WMI_DSTS_PRESENCE_BIT | ASUS_WMI_DSTS_USER_BIT))
2349 asus->driver->wlan_ctrl_by_user = 1;
2350
João Paulo Rechi Vitad1c4e9b2017-02-20 14:50:23 -05002351 if (!(asus->driver->wlan_ctrl_by_user && ashs_present())) {
João Paulo Rechi Vitaa977e592016-06-13 16:57:31 -04002352 err = asus_wmi_rfkill_init(asus);
2353 if (err)
2354 goto fail_rfkill;
2355 }
Corentin Charyba48fdb2010-11-29 08:14:07 +01002356
Oleksij Rempele9b61512017-04-28 16:19:49 +02002357 if (asus->driver->quirks->wmi_force_als_set)
2358 asus_wmi_set_als();
2359
Hans de Goeded8c66f62014-07-08 10:47:21 +02002360 /* Some Asus desktop boards export an acpi-video backlight interface,
2361 stop this from showing up */
2362 chassis_type = dmi_get_system_info(DMI_CHASSIS_TYPE);
2363 if (chassis_type && !strcmp(chassis_type, "3"))
Hans de Goede62c4aa12015-06-16 16:27:58 +02002364 acpi_video_set_dmi_backlight_type(acpi_backlight_vendor);
2365
AceLan Kao272c77d2012-06-13 09:32:06 +02002366 if (asus->driver->quirks->wmi_backlight_power)
Hans de Goede62c4aa12015-06-16 16:27:58 +02002367 acpi_video_set_dmi_backlight_type(acpi_backlight_vendor);
2368
zino lin999d4372016-08-28 16:12:06 +08002369 if (asus->driver->quirks->wmi_backlight_native)
2370 acpi_video_set_dmi_backlight_type(acpi_backlight_native);
2371
Kai-Chuan Hsieh8023eff2016-09-01 23:55:55 +08002372 if (asus->driver->quirks->xusb2pr)
2373 asus_wmi_set_xusb2pr(asus);
2374
Hans de Goede62c4aa12015-06-16 16:27:58 +02002375 if (acpi_video_get_backlight_type() == acpi_backlight_vendor) {
Corentin Charye12e6d92011-02-26 10:20:31 +01002376 err = asus_wmi_backlight_init(asus);
Corentin Charyb7187262011-02-06 13:28:39 +01002377 if (err && err != -ENODEV)
Corentin Chary27c136c2010-11-29 08:14:05 +01002378 goto fail_backlight;
Hans de Goede401fee82019-06-12 09:02:02 +02002379 } else if (asus->driver->quirks->wmi_backlight_set_devstate)
João Paulo Rechi Vita78f3ac72018-10-31 17:21:26 -07002380 err = asus_wmi_set_devstate(ASUS_WMI_DEVID_BACKLIGHT, 2, NULL);
Yong Wang45f2c692010-04-11 09:27:19 +08002381
Chris Chiu487579b2019-04-18 14:46:48 +08002382 if (asus_wmi_has_fnlock_key(asus)) {
2383 asus->fnlock_locked = true;
2384 asus_wmi_fnlock_update(asus);
2385 }
2386
Corentin Charye12e6d92011-02-26 10:20:31 +01002387 status = wmi_install_notify_handler(asus->driver->event_guid,
2388 asus_wmi_notify, asus);
Yong Wang45f2c692010-04-11 09:27:19 +08002389 if (ACPI_FAILURE(status)) {
Corentin Charye12e6d92011-02-26 10:20:31 +01002390 pr_err("Unable to register notify handler - %d\n", status);
Yong Wang45f2c692010-04-11 09:27:19 +08002391 err = -ENODEV;
Corentin Chary27c136c2010-11-29 08:14:05 +01002392 goto fail_wmi_handler;
Yong Wang45f2c692010-04-11 09:27:19 +08002393 }
2394
Greg Kroah-Hartmand2785d32019-06-12 14:12:52 +02002395 asus_wmi_debugfs_init(asus);
Corentin Chary8c1b2d82010-11-29 08:14:09 +01002396
Corentin Charya04ce292011-02-06 13:28:33 +01002397 return 0;
Yong Wang45f2c692010-04-11 09:27:19 +08002398
Corentin Chary27c136c2010-11-29 08:14:05 +01002399fail_wmi_handler:
Corentin Charye12e6d92011-02-26 10:20:31 +01002400 asus_wmi_backlight_exit(asus);
Corentin Chary27c136c2010-11-29 08:14:05 +01002401fail_backlight:
Corentin Charye12e6d92011-02-26 10:20:31 +01002402 asus_wmi_rfkill_exit(asus);
Corentin Charyba48fdb2010-11-29 08:14:07 +01002403fail_rfkill:
Corentin Charye12e6d92011-02-26 10:20:31 +01002404 asus_wmi_led_exit(asus);
Corentin Chary084fca62010-11-29 08:14:06 +01002405fail_leds:
Corentin Charye07babd2011-02-26 10:20:42 +01002406fail_hwmon:
Corentin Charye12e6d92011-02-26 10:20:31 +01002407 asus_wmi_input_exit(asus);
Corentin Chary27c136c2010-11-29 08:14:05 +01002408fail_input:
Yurii Pavlovskyib096f622019-05-14 21:07:05 +02002409 asus_wmi_sysfs_exit(asus->platform_device);
2410fail_sysfs:
Daniel Drake9af93db2019-07-17 13:10:58 +08002411fail_fan_boost_mode:
Corentin Chary27c136c2010-11-29 08:14:05 +01002412fail_platform:
Corentin Charye12e6d92011-02-26 10:20:31 +01002413 kfree(asus);
Corentin Charya04ce292011-02-06 13:28:33 +01002414 return err;
Yong Wang45f2c692010-04-11 09:27:19 +08002415}
2416
Corentin Charye12e6d92011-02-26 10:20:31 +01002417static int asus_wmi_remove(struct platform_device *device)
Yong Wang45f2c692010-04-11 09:27:19 +08002418{
Corentin Charye12e6d92011-02-26 10:20:31 +01002419 struct asus_wmi *asus;
Yong Wang45f2c692010-04-11 09:27:19 +08002420
Corentin Charye12e6d92011-02-26 10:20:31 +01002421 asus = platform_get_drvdata(device);
2422 wmi_remove_notify_handler(asus->driver->event_guid);
2423 asus_wmi_backlight_exit(asus);
2424 asus_wmi_input_exit(asus);
2425 asus_wmi_led_exit(asus);
2426 asus_wmi_rfkill_exit(asus);
2427 asus_wmi_debugfs_exit(asus);
Yurii Pavlovskyib096f622019-05-14 21:07:05 +02002428 asus_wmi_sysfs_exit(asus->platform_device);
Kast Bernd53e755c2015-05-13 16:24:16 +02002429 asus_hwmon_fan_set_auto(asus);
Yong Wang45f2c692010-04-11 09:27:19 +08002430
Corentin Charye12e6d92011-02-26 10:20:31 +01002431 kfree(asus);
Yong Wang45f2c692010-04-11 09:27:19 +08002432 return 0;
2433}
2434
Yurii Pavlovskyi54a31212019-05-14 21:04:48 +02002435/* Platform driver - hibernate/resume callbacks *******************************/
2436
Corentin Charye12e6d92011-02-26 10:20:31 +01002437static int asus_hotk_thaw(struct device *device)
Corentin Chary0773d7f2011-02-06 13:28:32 +01002438{
Corentin Charye12e6d92011-02-26 10:20:31 +01002439 struct asus_wmi *asus = dev_get_drvdata(device);
Corentin Chary0773d7f2011-02-06 13:28:32 +01002440
Corentin Charya7ce3f02011-02-26 10:20:33 +01002441 if (asus->wlan.rfkill) {
Corentin Chary0773d7f2011-02-06 13:28:32 +01002442 bool wlan;
2443
2444 /*
2445 * Work around bios bug - acpi _PTS turns off the wireless led
2446 * during suspend. Normally it restores it on resume, but
2447 * we should kick it ourselves in case hibernation is aborted.
2448 */
Corentin Chary1d070f82011-02-26 10:20:36 +01002449 wlan = asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_WLAN);
Corentin Charye12e6d92011-02-26 10:20:31 +01002450 asus_wmi_set_devstate(ASUS_WMI_DEVID_WLAN, wlan, NULL);
Corentin Chary0773d7f2011-02-06 13:28:32 +01002451 }
2452
2453 return 0;
2454}
2455
Oleksij Rempel30734042015-09-14 11:16:30 +02002456static int asus_hotk_resume(struct device *device)
2457{
2458 struct asus_wmi *asus = dev_get_drvdata(device);
2459
2460 if (!IS_ERR_OR_NULL(asus->kbd_led.dev))
Jian-Hong Pan9fe44fc2018-09-27 16:50:09 +08002461 kbd_led_update(asus);
Oleksij Rempel30734042015-09-14 11:16:30 +02002462
Chris Chiu487579b2019-04-18 14:46:48 +08002463 if (asus_wmi_has_fnlock_key(asus))
2464 asus_wmi_fnlock_update(asus);
Oleksij Rempel30734042015-09-14 11:16:30 +02002465 return 0;
2466}
2467
Corentin Charye12e6d92011-02-26 10:20:31 +01002468static int asus_hotk_restore(struct device *device)
Corentin Chary0773d7f2011-02-06 13:28:32 +01002469{
Corentin Charye12e6d92011-02-26 10:20:31 +01002470 struct asus_wmi *asus = dev_get_drvdata(device);
Corentin Chary0773d7f2011-02-06 13:28:32 +01002471 int bl;
2472
2473 /* Refresh both wlan rfkill state and pci hotplug */
Corentin Charya7ce3f02011-02-26 10:20:33 +01002474 if (asus->wlan.rfkill)
Corentin Charye12e6d92011-02-26 10:20:31 +01002475 asus_rfkill_hotplug(asus);
Corentin Chary0773d7f2011-02-06 13:28:32 +01002476
Corentin Charya7ce3f02011-02-26 10:20:33 +01002477 if (asus->bluetooth.rfkill) {
Corentin Chary1d070f82011-02-26 10:20:36 +01002478 bl = !asus_wmi_get_devstate_simple(asus,
2479 ASUS_WMI_DEVID_BLUETOOTH);
Corentin Charya7ce3f02011-02-26 10:20:33 +01002480 rfkill_set_sw_state(asus->bluetooth.rfkill, bl);
Corentin Chary2e9e1592011-02-06 13:28:37 +01002481 }
Corentin Charya7ce3f02011-02-26 10:20:33 +01002482 if (asus->wimax.rfkill) {
Corentin Chary1d070f82011-02-26 10:20:36 +01002483 bl = !asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_WIMAX);
Corentin Charya7ce3f02011-02-26 10:20:33 +01002484 rfkill_set_sw_state(asus->wimax.rfkill, bl);
Corentin Chary2e9e1592011-02-06 13:28:37 +01002485 }
Corentin Charya7ce3f02011-02-26 10:20:33 +01002486 if (asus->wwan3g.rfkill) {
Corentin Chary1d070f82011-02-26 10:20:36 +01002487 bl = !asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_WWAN3G);
Corentin Charya7ce3f02011-02-26 10:20:33 +01002488 rfkill_set_sw_state(asus->wwan3g.rfkill, bl);
Corentin Chary0773d7f2011-02-06 13:28:32 +01002489 }
Corentin Chary43be8bd2011-07-01 11:34:40 +02002490 if (asus->gps.rfkill) {
2491 bl = !asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_GPS);
2492 rfkill_set_sw_state(asus->gps.rfkill, bl);
2493 }
Corentin Charya912d322011-07-01 11:34:41 +02002494 if (asus->uwb.rfkill) {
2495 bl = !asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_UWB);
2496 rfkill_set_sw_state(asus->uwb.rfkill, bl);
2497 }
Oleksij Rempel30734042015-09-14 11:16:30 +02002498 if (!IS_ERR_OR_NULL(asus->kbd_led.dev))
Jian-Hong Pan9fe44fc2018-09-27 16:50:09 +08002499 kbd_led_update(asus);
Corentin Chary0773d7f2011-02-06 13:28:32 +01002500
Chris Chiu487579b2019-04-18 14:46:48 +08002501 if (asus_wmi_has_fnlock_key(asus))
2502 asus_wmi_fnlock_update(asus);
Corentin Chary0773d7f2011-02-06 13:28:32 +01002503 return 0;
2504}
2505
Corentin Charye12e6d92011-02-26 10:20:31 +01002506static const struct dev_pm_ops asus_pm_ops = {
2507 .thaw = asus_hotk_thaw,
2508 .restore = asus_hotk_restore,
Oleksij Rempel30734042015-09-14 11:16:30 +02002509 .resume = asus_hotk_resume,
Corentin Chary0773d7f2011-02-06 13:28:32 +01002510};
2511
Yurii Pavlovskyi54a31212019-05-14 21:04:48 +02002512/* Registration ***************************************************************/
2513
Corentin Charye12e6d92011-02-26 10:20:31 +01002514static int asus_wmi_probe(struct platform_device *pdev)
Corentin Charyd358cb52010-11-29 08:14:14 +01002515{
Corentin Charye12e6d92011-02-26 10:20:31 +01002516 struct platform_driver *pdrv = to_platform_driver(pdev->dev.driver);
2517 struct asus_wmi_driver *wdrv = to_asus_wmi_driver(pdrv);
2518 int ret;
Corentin Charyd358cb52010-11-29 08:14:14 +01002519
Corentin Charye12e6d92011-02-26 10:20:31 +01002520 if (!wmi_has_guid(ASUS_WMI_MGMT_GUID)) {
Hans de Goedec9946112019-01-21 14:24:36 +01002521 pr_warn("ASUS Management GUID not found\n");
Yong Wangee027e42010-03-21 10:26:34 +08002522 return -ENODEV;
2523 }
2524
Corentin Charye12e6d92011-02-26 10:20:31 +01002525 if (wdrv->event_guid && !wmi_has_guid(wdrv->event_guid)) {
Hans de Goedec9946112019-01-21 14:24:36 +01002526 pr_warn("ASUS Event GUID not found\n");
Corentin Charyd358cb52010-11-29 08:14:14 +01002527 return -ENODEV;
2528 }
2529
Corentin Charye12e6d92011-02-26 10:20:31 +01002530 if (wdrv->probe) {
2531 ret = wdrv->probe(pdev);
2532 if (ret)
2533 return ret;
2534 }
2535
2536 return asus_wmi_add(pdev);
Corentin Charya04ce292011-02-06 13:28:33 +01002537}
Yong Wangee027e42010-03-21 10:26:34 +08002538
Corentin Charye12e6d92011-02-26 10:20:31 +01002539static bool used;
Yong Wangee027e42010-03-21 10:26:34 +08002540
Corentin Chary8fe8c252011-07-01 11:34:32 +02002541int __init_or_module asus_wmi_register_driver(struct asus_wmi_driver *driver)
Corentin Charya04ce292011-02-06 13:28:33 +01002542{
Corentin Charye12e6d92011-02-26 10:20:31 +01002543 struct platform_driver *platform_driver;
2544 struct platform_device *platform_device;
2545
2546 if (used)
2547 return -EBUSY;
2548
2549 platform_driver = &driver->platform_driver;
2550 platform_driver->remove = asus_wmi_remove;
2551 platform_driver->driver.owner = driver->owner;
2552 platform_driver->driver.name = driver->name;
2553 platform_driver->driver.pm = &asus_pm_ops;
2554
2555 platform_device = platform_create_bundle(platform_driver,
2556 asus_wmi_probe,
Corentin Charya04ce292011-02-06 13:28:33 +01002557 NULL, 0, NULL, 0);
2558 if (IS_ERR(platform_device))
2559 return PTR_ERR(platform_device);
Corentin Charye12e6d92011-02-26 10:20:31 +01002560
2561 used = true;
2562 return 0;
2563}
2564EXPORT_SYMBOL_GPL(asus_wmi_register_driver);
2565
2566void asus_wmi_unregister_driver(struct asus_wmi_driver *driver)
2567{
2568 platform_device_unregister(driver->platform_device);
2569 platform_driver_unregister(&driver->platform_driver);
2570 used = false;
2571}
2572EXPORT_SYMBOL_GPL(asus_wmi_unregister_driver);
2573
2574static int __init asus_wmi_init(void)
2575{
vic0ed60652013-05-22 21:32:10 +03002576 pr_info("ASUS WMI generic driver loaded\n");
Yong Wangee027e42010-03-21 10:26:34 +08002577 return 0;
2578}
2579
Corentin Charye12e6d92011-02-26 10:20:31 +01002580static void __exit asus_wmi_exit(void)
Yong Wangee027e42010-03-21 10:26:34 +08002581{
vic0ed60652013-05-22 21:32:10 +03002582 pr_info("ASUS WMI generic driver unloaded\n");
Yong Wangee027e42010-03-21 10:26:34 +08002583}
2584
Corentin Charye12e6d92011-02-26 10:20:31 +01002585module_init(asus_wmi_init);
2586module_exit(asus_wmi_exit);