blob: 9b18a184e0aac89c58232681bbd5ab689cae0e2f [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
Yong Wangee027e42010-03-21 10:26:34 +080060
Chris Chiu487579b2019-04-18 14:46:48 +080061#define ASUS_WMI_FNLOCK_BIOS_DISABLED BIT(0)
62
Kast Bernd53e755c2015-05-13 16:24:16 +020063#define ASUS_FAN_DESC "cpu_fan"
64#define ASUS_FAN_MFUN 0x13
65#define ASUS_FAN_SFUN_READ 0x06
66#define ASUS_FAN_SFUN_WRITE 0x07
67#define ASUS_FAN_CTRL_MANUAL 1
68#define ASUS_FAN_CTRL_AUTO 2
69
Kai-Chuan Hsieh8023eff2016-09-01 23:55:55 +080070#define USB_INTEL_XUSB2PR 0xD0
71#define PCI_DEVICE_ID_INTEL_LYNXPOINT_LP_XHCI 0x9c31
72
João Paulo Rechi Vita71050ae2017-02-20 14:50:22 -050073static const char * const ashs_ids[] = { "ATK4001", "ATK4002", NULL };
74
João Paulo Rechi Vitacf48bf92018-05-22 14:30:15 -070075static bool ashs_present(void)
76{
77 int i = 0;
78 while (ashs_ids[i]) {
79 if (acpi_dev_found(ashs_ids[i++]))
80 return true;
81 }
82 return false;
83}
84
Yong Wang3d7b1652010-04-11 09:27:54 +080085struct bios_args {
Corentin Charyd33da3b2011-02-26 10:20:35 +010086 u32 arg0;
87 u32 arg1;
88} __packed;
Yong Wang3d7b1652010-04-11 09:27:54 +080089
Corentin Chary8c1b2d82010-11-29 08:14:09 +010090/*
Kast Bernd53e755c2015-05-13 16:24:16 +020091 * Struct that's used for all methods called via AGFN. Naming is
92 * identically to the AML code.
93 */
94struct agfn_args {
95 u16 mfun; /* probably "Multi-function" to be called */
96 u16 sfun; /* probably "Sub-function" to be called */
97 u16 len; /* size of the hole struct, including subfunction fields */
98 u8 stas; /* not used by now */
99 u8 err; /* zero on success */
100} __packed;
101
102/* struct used for calling fan read and write methods */
103struct fan_args {
104 struct agfn_args agfn; /* common fields */
105 u8 fan; /* fan number: 0: set auto mode 1: 1st fan */
106 u32 speed; /* read: RPM/100 - write: 0-255 */
107} __packed;
108
109/*
Corentin Charye12e6d92011-02-26 10:20:31 +0100110 * <platform>/ - debugfs root directory
Corentin Chary8c1b2d82010-11-29 08:14:09 +0100111 * dev_id - current dev_id
112 * ctrl_param - current ctrl_param
Corentin Charyef343492011-02-26 10:20:39 +0100113 * method_id - current method_id
Corentin Chary8c1b2d82010-11-29 08:14:09 +0100114 * devs - call DEVS(dev_id, ctrl_param) and print result
115 * dsts - call DSTS(dev_id) and print result
Corentin Charyef343492011-02-26 10:20:39 +0100116 * call - call method_id(dev_id, ctrl_param) and print result
Corentin Chary8c1b2d82010-11-29 08:14:09 +0100117 */
Corentin Charye12e6d92011-02-26 10:20:31 +0100118struct asus_wmi_debug {
Corentin Chary8c1b2d82010-11-29 08:14:09 +0100119 struct dentry *root;
Corentin Charyef343492011-02-26 10:20:39 +0100120 u32 method_id;
Corentin Chary8c1b2d82010-11-29 08:14:09 +0100121 u32 dev_id;
122 u32 ctrl_param;
123};
124
Corentin Charya7ce3f02011-02-26 10:20:33 +0100125struct asus_rfkill {
126 struct asus_wmi *asus;
127 struct rfkill *rfkill;
128 u32 dev_id;
129};
130
Corentin Charye12e6d92011-02-26 10:20:31 +0100131struct asus_wmi {
Corentin Chary1d070f82011-02-26 10:20:36 +0100132 int dsts_id;
Corentin Chary46dbca82011-02-26 10:20:38 +0100133 int spec;
134 int sfun;
Corentin Chary1d070f82011-02-26 10:20:36 +0100135
Yong Wang81248882010-04-11 09:26:33 +0800136 struct input_dev *inputdev;
Yong Wang3d7b1652010-04-11 09:27:54 +0800137 struct backlight_device *backlight_device;
Corentin Chary27c136c2010-11-29 08:14:05 +0100138 struct platform_device *platform_device;
Corentin Chary084fca62010-11-29 08:14:06 +0100139
AceLan Kao6cae06e2012-07-27 16:51:59 +0800140 struct led_classdev wlan_led;
141 int wlan_led_wk;
Corentin Chary084fca62010-11-29 08:14:06 +0100142 struct led_classdev tpd_led;
143 int tpd_led_wk;
Corentin Charye9809c02011-07-01 11:34:31 +0200144 struct led_classdev kbd_led;
145 int kbd_led_wk;
Maxime Bellengé4c059842017-09-23 18:23:37 +0200146 struct led_classdev lightbar_led;
147 int lightbar_led_wk;
Corentin Chary084fca62010-11-29 08:14:06 +0100148 struct workqueue_struct *led_workqueue;
149 struct work_struct tpd_led_work;
AceLan Kao6cae06e2012-07-27 16:51:59 +0800150 struct work_struct wlan_led_work;
Maxime Bellengé4c059842017-09-23 18:23:37 +0200151 struct work_struct lightbar_led_work;
Corentin Charyba48fdb2010-11-29 08:14:07 +0100152
Corentin Charya7ce3f02011-02-26 10:20:33 +0100153 struct asus_rfkill wlan;
154 struct asus_rfkill bluetooth;
155 struct asus_rfkill wimax;
156 struct asus_rfkill wwan3g;
Corentin Chary43be8bd2011-07-01 11:34:40 +0200157 struct asus_rfkill gps;
Corentin Charya912d322011-07-01 11:34:41 +0200158 struct asus_rfkill uwb;
Corentin Chary8c1b2d82010-11-29 08:14:09 +0100159
Kast Bernd53e755c2015-05-13 16:24:16 +0200160 bool asus_hwmon_fan_manual_mode;
161 int asus_hwmon_num_fans;
162 int asus_hwmon_pwm;
163
Lukas Wunner125450f2018-09-08 09:59:01 +0200164 struct hotplug_slot hotplug_slot;
Corentin Charyafa7c882011-02-06 13:28:28 +0100165 struct mutex hotplug_lock;
Corentin Chary279f8f92011-02-06 13:28:29 +0100166 struct mutex wmi_lock;
167 struct workqueue_struct *hotplug_workqueue;
168 struct work_struct hotplug_work;
Corentin Charyafa7c882011-02-06 13:28:28 +0100169
Chris Chiu487579b2019-04-18 14:46:48 +0800170 bool fnlock_locked;
171
Corentin Charye12e6d92011-02-26 10:20:31 +0100172 struct asus_wmi_debug debug;
173
174 struct asus_wmi_driver *driver;
Yong Wang81248882010-04-11 09:26:33 +0800175};
176
Corentin Charye12e6d92011-02-26 10:20:31 +0100177static int asus_wmi_input_init(struct asus_wmi *asus)
Yong Wangee027e42010-03-21 10:26:34 +0800178{
179 int err;
180
Corentin Charye12e6d92011-02-26 10:20:31 +0100181 asus->inputdev = input_allocate_device();
182 if (!asus->inputdev)
Yong Wangee027e42010-03-21 10:26:34 +0800183 return -ENOMEM;
184
Corentin Chary58a9f392011-03-30 16:32:32 +0200185 asus->inputdev->name = asus->driver->input_name;
186 asus->inputdev->phys = asus->driver->input_phys;
Corentin Charye12e6d92011-02-26 10:20:31 +0100187 asus->inputdev->id.bustype = BUS_HOST;
188 asus->inputdev->dev.parent = &asus->platform_device->dev;
Seth Forshee39bbde02011-07-04 09:49:20 +0200189 set_bit(EV_REP, asus->inputdev->evbit);
Yong Wangee027e42010-03-21 10:26:34 +0800190
Corentin Charye12e6d92011-02-26 10:20:31 +0100191 err = sparse_keymap_setup(asus->inputdev, asus->driver->keymap, NULL);
Yong Wangee027e42010-03-21 10:26:34 +0800192 if (err)
193 goto err_free_dev;
194
Corentin Charye12e6d92011-02-26 10:20:31 +0100195 err = input_register_device(asus->inputdev);
Yong Wangee027e42010-03-21 10:26:34 +0800196 if (err)
Michał Kępieńc2ef3a12017-03-09 13:11:39 +0100197 goto err_free_dev;
Yong Wangee027e42010-03-21 10:26:34 +0800198
199 return 0;
200
Yong Wangee027e42010-03-21 10:26:34 +0800201err_free_dev:
Corentin Charye12e6d92011-02-26 10:20:31 +0100202 input_free_device(asus->inputdev);
Yong Wangee027e42010-03-21 10:26:34 +0800203 return err;
204}
205
Corentin Charye12e6d92011-02-26 10:20:31 +0100206static void asus_wmi_input_exit(struct asus_wmi *asus)
Yong Wang81248882010-04-11 09:26:33 +0800207{
Michał Kępieńc2ef3a12017-03-09 13:11:39 +0100208 if (asus->inputdev)
Corentin Charye12e6d92011-02-26 10:20:31 +0100209 input_unregister_device(asus->inputdev);
Yong Wang81248882010-04-11 09:26:33 +0800210
Corentin Charye12e6d92011-02-26 10:20:31 +0100211 asus->inputdev = NULL;
Yong Wang81248882010-04-11 09:26:33 +0800212}
213
Daniel Drakeffb6ce72018-10-09 14:40:55 +0800214int asus_wmi_evaluate_method(u32 method_id, u32 arg0, u32 arg1, u32 *retval)
Yong Wang3d7b1652010-04-11 09:27:54 +0800215{
Corentin Charyd33da3b2011-02-26 10:20:35 +0100216 struct bios_args args = {
217 .arg0 = arg0,
218 .arg1 = arg1,
219 };
220 struct acpi_buffer input = { (acpi_size) sizeof(args), &args };
Yong Wang3d7b1652010-04-11 09:27:54 +0800221 struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER, NULL };
Yong Wang3d7b1652010-04-11 09:27:54 +0800222 acpi_status status;
Corentin Charyd33da3b2011-02-26 10:20:35 +0100223 union acpi_object *obj;
Rickard Strandqvist8ad3be12014-06-01 14:58:52 +0200224 u32 tmp = 0;
Yong Wang3d7b1652010-04-11 09:27:54 +0800225
Pali Rohár0fe57262017-08-12 09:44:16 +0200226 status = wmi_evaluate_method(ASUS_WMI_MGMT_GUID, 0, method_id,
Corentin Charyafa7c882011-02-06 13:28:28 +0100227 &input, &output);
Yong Wang3d7b1652010-04-11 09:27:54 +0800228
229 if (ACPI_FAILURE(status))
Corentin Charyd33da3b2011-02-26 10:20:35 +0100230 goto exit;
Yong Wang3d7b1652010-04-11 09:27:54 +0800231
232 obj = (union acpi_object *)output.pointer;
233 if (obj && obj->type == ACPI_TYPE_INTEGER)
Corentin Charye12e6d92011-02-26 10:20:31 +0100234 tmp = (u32) obj->integer.value;
Yong Wang3d7b1652010-04-11 09:27:54 +0800235
Corentin Chary2a3f0062010-11-29 08:14:10 +0100236 if (retval)
237 *retval = tmp;
Yong Wang3d7b1652010-04-11 09:27:54 +0800238
239 kfree(obj);
240
Corentin Charyd33da3b2011-02-26 10:20:35 +0100241exit:
242 if (ACPI_FAILURE(status))
243 return -EIO;
Yong Wang3d7b1652010-04-11 09:27:54 +0800244
Corentin Charyd33da3b2011-02-26 10:20:35 +0100245 if (tmp == ASUS_WMI_UNSUPPORTED_METHOD)
246 return -ENODEV;
247
248 return 0;
Yong Wang3d7b1652010-04-11 09:27:54 +0800249}
Daniel Drakeffb6ce72018-10-09 14:40:55 +0800250EXPORT_SYMBOL_GPL(asus_wmi_evaluate_method);
Yong Wang3d7b1652010-04-11 09:27:54 +0800251
Kast Bernd53e755c2015-05-13 16:24:16 +0200252static int asus_wmi_evaluate_method_agfn(const struct acpi_buffer args)
253{
254 struct acpi_buffer input;
255 u64 phys_addr;
256 u32 retval;
257 u32 status = -1;
258
259 /*
260 * Copy to dma capable address otherwise memory corruption occurs as
261 * bios has to be able to access it.
262 */
263 input.pointer = kzalloc(args.length, GFP_DMA | GFP_KERNEL);
264 input.length = args.length;
265 if (!input.pointer)
266 return -ENOMEM;
267 phys_addr = virt_to_phys(input.pointer);
268 memcpy(input.pointer, args.pointer, args.length);
269
270 status = asus_wmi_evaluate_method(ASUS_WMI_METHODID_AGFN,
271 phys_addr, 0, &retval);
272 if (!status)
273 memcpy(args.pointer, input.pointer, args.length);
274
275 kfree(input.pointer);
276 if (status)
277 return -ENXIO;
278
279 return retval;
280}
281
Corentin Chary1d070f82011-02-26 10:20:36 +0100282static int asus_wmi_get_devstate(struct asus_wmi *asus, u32 dev_id, u32 *retval)
Corentin Charyd33da3b2011-02-26 10:20:35 +0100283{
Corentin Chary1d070f82011-02-26 10:20:36 +0100284 return asus_wmi_evaluate_method(asus->dsts_id, dev_id, 0, retval);
Corentin Charyd33da3b2011-02-26 10:20:35 +0100285}
286
287static int asus_wmi_set_devstate(u32 dev_id, u32 ctrl_param,
Corentin Chary1d070f82011-02-26 10:20:36 +0100288 u32 *retval)
Yong Wang3d7b1652010-04-11 09:27:54 +0800289{
Corentin Charyd33da3b2011-02-26 10:20:35 +0100290 return asus_wmi_evaluate_method(ASUS_WMI_METHODID_DEVS, dev_id,
291 ctrl_param, retval);
Yong Wang3d7b1652010-04-11 09:27:54 +0800292}
293
Corentin Chary5c956382011-02-06 13:28:31 +0100294/* Helper for special devices with magic return codes */
Corentin Chary1d070f82011-02-26 10:20:36 +0100295static int asus_wmi_get_devstate_bits(struct asus_wmi *asus,
296 u32 dev_id, u32 mask)
Corentin Chary5c956382011-02-06 13:28:31 +0100297{
298 u32 retval = 0;
Corentin Charyd33da3b2011-02-26 10:20:35 +0100299 int err;
Corentin Chary5c956382011-02-06 13:28:31 +0100300
Corentin Chary1d070f82011-02-26 10:20:36 +0100301 err = asus_wmi_get_devstate(asus, dev_id, &retval);
Corentin Chary5c956382011-02-06 13:28:31 +0100302
Corentin Charyd33da3b2011-02-26 10:20:35 +0100303 if (err < 0)
304 return err;
Corentin Chary5c956382011-02-06 13:28:31 +0100305
Corentin Charye12e6d92011-02-26 10:20:31 +0100306 if (!(retval & ASUS_WMI_DSTS_PRESENCE_BIT))
Corentin Chary5c956382011-02-06 13:28:31 +0100307 return -ENODEV;
308
Corentin Charya75fe0d2011-02-26 10:20:34 +0100309 if (mask == ASUS_WMI_DSTS_STATUS_BIT) {
310 if (retval & ASUS_WMI_DSTS_UNKNOWN_BIT)
311 return -ENODEV;
312 }
313
Corentin Charyb7187262011-02-06 13:28:39 +0100314 return retval & mask;
315}
316
Corentin Chary1d070f82011-02-26 10:20:36 +0100317static int asus_wmi_get_devstate_simple(struct asus_wmi *asus, u32 dev_id)
Corentin Charyb7187262011-02-06 13:28:39 +0100318{
Corentin Chary1d070f82011-02-26 10:20:36 +0100319 return asus_wmi_get_devstate_bits(asus, dev_id,
320 ASUS_WMI_DSTS_STATUS_BIT);
Corentin Chary5c956382011-02-06 13:28:31 +0100321}
322
Corentin Chary084fca62010-11-29 08:14:06 +0100323/*
324 * LEDs
325 */
326/*
327 * These functions actually update the LED's, and are called from a
328 * workqueue. By doing this as separate work rather than when the LED
Corentin Charye12e6d92011-02-26 10:20:31 +0100329 * subsystem asks, we avoid messing with the Asus ACPI stuff during a
Corentin Chary084fca62010-11-29 08:14:06 +0100330 * potentially bad time, such as a timer interrupt.
331 */
332static void tpd_led_update(struct work_struct *work)
333{
334 int ctrl_param;
Corentin Charye12e6d92011-02-26 10:20:31 +0100335 struct asus_wmi *asus;
Corentin Chary084fca62010-11-29 08:14:06 +0100336
Corentin Charye12e6d92011-02-26 10:20:31 +0100337 asus = container_of(work, struct asus_wmi, tpd_led_work);
Corentin Chary084fca62010-11-29 08:14:06 +0100338
Corentin Charye12e6d92011-02-26 10:20:31 +0100339 ctrl_param = asus->tpd_led_wk;
340 asus_wmi_set_devstate(ASUS_WMI_DEVID_TOUCHPAD_LED, ctrl_param, NULL);
Corentin Chary084fca62010-11-29 08:14:06 +0100341}
342
343static void tpd_led_set(struct led_classdev *led_cdev,
344 enum led_brightness value)
345{
Corentin Charye12e6d92011-02-26 10:20:31 +0100346 struct asus_wmi *asus;
Corentin Chary084fca62010-11-29 08:14:06 +0100347
Corentin Charye12e6d92011-02-26 10:20:31 +0100348 asus = container_of(led_cdev, struct asus_wmi, tpd_led);
Corentin Chary084fca62010-11-29 08:14:06 +0100349
Corentin Charye12e6d92011-02-26 10:20:31 +0100350 asus->tpd_led_wk = !!value;
351 queue_work(asus->led_workqueue, &asus->tpd_led_work);
Corentin Chary084fca62010-11-29 08:14:06 +0100352}
353
Corentin Charye12e6d92011-02-26 10:20:31 +0100354static int read_tpd_led_state(struct asus_wmi *asus)
Corentin Chary084fca62010-11-29 08:14:06 +0100355{
Corentin Chary1d070f82011-02-26 10:20:36 +0100356 return asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_TOUCHPAD_LED);
Corentin Chary084fca62010-11-29 08:14:06 +0100357}
358
359static enum led_brightness tpd_led_get(struct led_classdev *led_cdev)
360{
Corentin Charye12e6d92011-02-26 10:20:31 +0100361 struct asus_wmi *asus;
Corentin Chary084fca62010-11-29 08:14:06 +0100362
Corentin Charye12e6d92011-02-26 10:20:31 +0100363 asus = container_of(led_cdev, struct asus_wmi, tpd_led);
Corentin Chary084fca62010-11-29 08:14:06 +0100364
Corentin Charye12e6d92011-02-26 10:20:31 +0100365 return read_tpd_led_state(asus);
Corentin Chary084fca62010-11-29 08:14:06 +0100366}
367
Jian-Hong Pan9fe44fc2018-09-27 16:50:09 +0800368static void kbd_led_update(struct asus_wmi *asus)
Corentin Chary084fca62010-11-29 08:14:06 +0100369{
Corentin Charye9809c02011-07-01 11:34:31 +0200370 int ctrl_param = 0;
Corentin Chary084fca62010-11-29 08:14:06 +0100371
Corentin Charye9809c02011-07-01 11:34:31 +0200372 /*
373 * bits 0-2: level
374 * bit 7: light on/off
375 */
376 if (asus->kbd_led_wk > 0)
377 ctrl_param = 0x80 | (asus->kbd_led_wk & 0x7F);
Corentin Chary084fca62010-11-29 08:14:06 +0100378
Corentin Charye9809c02011-07-01 11:34:31 +0200379 asus_wmi_set_devstate(ASUS_WMI_DEVID_KBD_BACKLIGHT, ctrl_param, NULL);
380}
Corentin Chary084fca62010-11-29 08:14:06 +0100381
Corentin Charye9809c02011-07-01 11:34:31 +0200382static int kbd_led_read(struct asus_wmi *asus, int *level, int *env)
383{
384 int retval;
385
386 /*
387 * bits 0-2: level
388 * bit 7: light on/off
389 * bit 8-10: environment (0: dark, 1: normal, 2: light)
390 * bit 17: status unknown
391 */
392 retval = asus_wmi_get_devstate_bits(asus, ASUS_WMI_DEVID_KBD_BACKLIGHT,
393 0xFFFF);
394
Corentin Charyaf965e92011-07-01 11:34:34 +0200395 /* Unknown status is considered as off */
Corentin Charye9809c02011-07-01 11:34:31 +0200396 if (retval == 0x8000)
Corentin Charyaf965e92011-07-01 11:34:34 +0200397 retval = 0;
Corentin Charye9809c02011-07-01 11:34:31 +0200398
399 if (retval >= 0) {
400 if (level)
Corentin Charyc09b2232012-03-20 09:53:04 +0100401 *level = retval & 0x7F;
Corentin Charye9809c02011-07-01 11:34:31 +0200402 if (env)
403 *env = (retval >> 8) & 0x7F;
404 retval = 0;
Corentin Chary084fca62010-11-29 08:14:06 +0100405 }
406
Corentin Charye9809c02011-07-01 11:34:31 +0200407 return retval;
408}
409
Chris Chiudbb3d782018-06-20 22:46:44 +0800410static void do_kbd_led_set(struct led_classdev *led_cdev, int value)
Corentin Charye9809c02011-07-01 11:34:31 +0200411{
412 struct asus_wmi *asus;
Chris Chiudbb3d782018-06-20 22:46:44 +0800413 int max_level;
Corentin Charye9809c02011-07-01 11:34:31 +0200414
415 asus = container_of(led_cdev, struct asus_wmi, kbd_led);
Chris Chiudbb3d782018-06-20 22:46:44 +0800416 max_level = asus->kbd_led.max_brightness;
Corentin Charye9809c02011-07-01 11:34:31 +0200417
Chris Chiudbb3d782018-06-20 22:46:44 +0800418 if (value > max_level)
419 value = max_level;
Corentin Charye9809c02011-07-01 11:34:31 +0200420 else if (value < 0)
421 value = 0;
422
423 asus->kbd_led_wk = value;
Jian-Hong Pan9fe44fc2018-09-27 16:50:09 +0800424 kbd_led_update(asus);
Corentin Charye9809c02011-07-01 11:34:31 +0200425}
426
Chris Chiudbb3d782018-06-20 22:46:44 +0800427static void kbd_led_set(struct led_classdev *led_cdev,
428 enum led_brightness value)
429{
430 do_kbd_led_set(led_cdev, value);
431}
432
Jian-Hong Pan29f6eb52018-10-22 18:00:04 +0800433static void kbd_led_set_by_kbd(struct asus_wmi *asus, enum led_brightness value)
434{
435 struct led_classdev *led_cdev = &asus->kbd_led;
436
437 do_kbd_led_set(led_cdev, value);
438 led_classdev_notify_brightness_hw_changed(led_cdev, asus->kbd_led_wk);
439}
440
Corentin Charye9809c02011-07-01 11:34:31 +0200441static enum led_brightness kbd_led_get(struct led_classdev *led_cdev)
442{
443 struct asus_wmi *asus;
444 int retval, value;
445
446 asus = container_of(led_cdev, struct asus_wmi, kbd_led);
447
448 retval = kbd_led_read(asus, &value, NULL);
449
450 if (retval < 0)
451 return retval;
452
453 return value;
Corentin Chary084fca62010-11-29 08:14:06 +0100454}
455
AceLan Kao6cae06e2012-07-27 16:51:59 +0800456static int wlan_led_unknown_state(struct asus_wmi *asus)
457{
458 u32 result;
459
460 asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_WIRELESS_LED, &result);
461
462 return result & ASUS_WMI_DSTS_UNKNOWN_BIT;
463}
464
465static int wlan_led_presence(struct asus_wmi *asus)
466{
467 u32 result;
468
469 asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_WIRELESS_LED, &result);
470
471 return result & ASUS_WMI_DSTS_PRESENCE_BIT;
472}
473
474static void wlan_led_update(struct work_struct *work)
475{
476 int ctrl_param;
477 struct asus_wmi *asus;
478
479 asus = container_of(work, struct asus_wmi, wlan_led_work);
480
481 ctrl_param = asus->wlan_led_wk;
482 asus_wmi_set_devstate(ASUS_WMI_DEVID_WIRELESS_LED, ctrl_param, NULL);
483}
484
485static void wlan_led_set(struct led_classdev *led_cdev,
486 enum led_brightness value)
487{
488 struct asus_wmi *asus;
489
490 asus = container_of(led_cdev, struct asus_wmi, wlan_led);
491
492 asus->wlan_led_wk = !!value;
493 queue_work(asus->led_workqueue, &asus->wlan_led_work);
494}
495
496static enum led_brightness wlan_led_get(struct led_classdev *led_cdev)
497{
498 struct asus_wmi *asus;
499 u32 result;
500
501 asus = container_of(led_cdev, struct asus_wmi, wlan_led);
502 asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_WIRELESS_LED, &result);
503
504 return result & ASUS_WMI_DSTS_BRIGHTNESS_MASK;
505}
506
Maxime Bellengé4c059842017-09-23 18:23:37 +0200507static void lightbar_led_update(struct work_struct *work)
508{
509 struct asus_wmi *asus;
510 int ctrl_param;
511
512 asus = container_of(work, struct asus_wmi, lightbar_led_work);
513
514 ctrl_param = asus->lightbar_led_wk;
515 asus_wmi_set_devstate(ASUS_WMI_DEVID_LIGHTBAR, ctrl_param, NULL);
516}
517
518static void lightbar_led_set(struct led_classdev *led_cdev,
519 enum led_brightness value)
520{
521 struct asus_wmi *asus;
522
523 asus = container_of(led_cdev, struct asus_wmi, lightbar_led);
524
525 asus->lightbar_led_wk = !!value;
526 queue_work(asus->led_workqueue, &asus->lightbar_led_work);
527}
528
529static enum led_brightness lightbar_led_get(struct led_classdev *led_cdev)
530{
531 struct asus_wmi *asus;
532 u32 result;
533
534 asus = container_of(led_cdev, struct asus_wmi, lightbar_led);
535 asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_LIGHTBAR, &result);
536
537 return result & ASUS_WMI_DSTS_LIGHTBAR_MASK;
538}
539
540static int lightbar_led_presence(struct asus_wmi *asus)
541{
542 u32 result;
543
544 asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_LIGHTBAR, &result);
545
546 return result & ASUS_WMI_DSTS_PRESENCE_BIT;
547}
548
Corentin Charye12e6d92011-02-26 10:20:31 +0100549static void asus_wmi_led_exit(struct asus_wmi *asus)
Corentin Chary084fca62010-11-29 08:14:06 +0100550{
Axel Line9298022011-08-08 17:16:01 +0800551 if (!IS_ERR_OR_NULL(asus->kbd_led.dev))
552 led_classdev_unregister(&asus->kbd_led);
553 if (!IS_ERR_OR_NULL(asus->tpd_led.dev))
Corentin Charye12e6d92011-02-26 10:20:31 +0100554 led_classdev_unregister(&asus->tpd_led);
AceLan Kao6cae06e2012-07-27 16:51:59 +0800555 if (!IS_ERR_OR_NULL(asus->wlan_led.dev))
556 led_classdev_unregister(&asus->wlan_led);
Maxime Bellengé4c059842017-09-23 18:23:37 +0200557 if (!IS_ERR_OR_NULL(asus->lightbar_led.dev))
558 led_classdev_unregister(&asus->lightbar_led);
Corentin Charye12e6d92011-02-26 10:20:31 +0100559 if (asus->led_workqueue)
560 destroy_workqueue(asus->led_workqueue);
Corentin Chary084fca62010-11-29 08:14:06 +0100561}
562
Corentin Charye9809c02011-07-01 11:34:31 +0200563static int asus_wmi_led_init(struct asus_wmi *asus)
564{
Oleksij Rempel30734042015-09-14 11:16:30 +0200565 int rv = 0, led_val;
Corentin Charye9809c02011-07-01 11:34:31 +0200566
567 asus->led_workqueue = create_singlethread_workqueue("led_workqueue");
568 if (!asus->led_workqueue)
569 return -ENOMEM;
570
571 if (read_tpd_led_state(asus) >= 0) {
572 INIT_WORK(&asus->tpd_led_work, tpd_led_update);
573
574 asus->tpd_led.name = "asus::touchpad";
575 asus->tpd_led.brightness_set = tpd_led_set;
576 asus->tpd_led.brightness_get = tpd_led_get;
577 asus->tpd_led.max_brightness = 1;
578
579 rv = led_classdev_register(&asus->platform_device->dev,
580 &asus->tpd_led);
581 if (rv)
582 goto error;
583 }
584
Oleksij Rempel30734042015-09-14 11:16:30 +0200585 led_val = kbd_led_read(asus, NULL, NULL);
586 if (led_val >= 0) {
Oleksij Rempel30734042015-09-14 11:16:30 +0200587 asus->kbd_led_wk = led_val;
Corentin Charye9809c02011-07-01 11:34:31 +0200588 asus->kbd_led.name = "asus::kbd_backlight";
Chris Chiudbb3d782018-06-20 22:46:44 +0800589 asus->kbd_led.flags = LED_BRIGHT_HW_CHANGED;
Corentin Charye9809c02011-07-01 11:34:31 +0200590 asus->kbd_led.brightness_set = kbd_led_set;
591 asus->kbd_led.brightness_get = kbd_led_get;
592 asus->kbd_led.max_brightness = 3;
593
594 rv = led_classdev_register(&asus->platform_device->dev,
595 &asus->kbd_led);
AceLan Kao6cae06e2012-07-27 16:51:59 +0800596 if (rv)
597 goto error;
598 }
599
AceLan Kaof5156232014-07-09 16:18:18 +0800600 if (wlan_led_presence(asus) && (asus->driver->quirks->wapf > 0)) {
AceLan Kao6cae06e2012-07-27 16:51:59 +0800601 INIT_WORK(&asus->wlan_led_work, wlan_led_update);
602
603 asus->wlan_led.name = "asus::wlan";
604 asus->wlan_led.brightness_set = wlan_led_set;
605 if (!wlan_led_unknown_state(asus))
606 asus->wlan_led.brightness_get = wlan_led_get;
607 asus->wlan_led.flags = LED_CORE_SUSPENDRESUME;
608 asus->wlan_led.max_brightness = 1;
609 asus->wlan_led.default_trigger = "asus-wlan";
610
611 rv = led_classdev_register(&asus->platform_device->dev,
612 &asus->wlan_led);
Maxime Bellengé4c059842017-09-23 18:23:37 +0200613 if (rv)
614 goto error;
615 }
616
617 if (lightbar_led_presence(asus)) {
618 INIT_WORK(&asus->lightbar_led_work, lightbar_led_update);
619
620 asus->lightbar_led.name = "asus::lightbar";
621 asus->lightbar_led.brightness_set = lightbar_led_set;
622 asus->lightbar_led.brightness_get = lightbar_led_get;
623 asus->lightbar_led.max_brightness = 1;
624
625 rv = led_classdev_register(&asus->platform_device->dev,
626 &asus->lightbar_led);
Corentin Charye9809c02011-07-01 11:34:31 +0200627 }
628
629error:
630 if (rv)
631 asus_wmi_led_exit(asus);
632
633 return rv;
634}
635
636
Corentin Chary084fca62010-11-29 08:14:06 +0100637/*
Corentin Charyafa7c882011-02-06 13:28:28 +0100638 * PCI hotplug (for wlan rfkill)
639 */
Corentin Charye12e6d92011-02-26 10:20:31 +0100640static bool asus_wlan_rfkill_blocked(struct asus_wmi *asus)
Corentin Charyafa7c882011-02-06 13:28:28 +0100641{
Corentin Chary1d070f82011-02-26 10:20:36 +0100642 int result = asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_WLAN);
Corentin Charyafa7c882011-02-06 13:28:28 +0100643
Corentin Chary5c956382011-02-06 13:28:31 +0100644 if (result < 0)
Corentin Charyafa7c882011-02-06 13:28:28 +0100645 return false;
Corentin Chary5c956382011-02-06 13:28:31 +0100646 return !result;
Corentin Charyafa7c882011-02-06 13:28:28 +0100647}
648
Corentin Charye12e6d92011-02-26 10:20:31 +0100649static void asus_rfkill_hotplug(struct asus_wmi *asus)
Corentin Charyafa7c882011-02-06 13:28:28 +0100650{
651 struct pci_dev *dev;
652 struct pci_bus *bus;
Corentin Chary279f8f92011-02-06 13:28:29 +0100653 bool blocked;
Corentin Charyafa7c882011-02-06 13:28:28 +0100654 bool absent;
655 u32 l;
656
Corentin Charye12e6d92011-02-26 10:20:31 +0100657 mutex_lock(&asus->wmi_lock);
658 blocked = asus_wlan_rfkill_blocked(asus);
659 mutex_unlock(&asus->wmi_lock);
Corentin Charyafa7c882011-02-06 13:28:28 +0100660
Corentin Charye12e6d92011-02-26 10:20:31 +0100661 mutex_lock(&asus->hotplug_lock);
Rafael J. Wysocki8b9ec1d2014-01-10 15:27:08 +0100662 pci_lock_rescan_remove();
Corentin Charyafa7c882011-02-06 13:28:28 +0100663
Corentin Charya7ce3f02011-02-26 10:20:33 +0100664 if (asus->wlan.rfkill)
665 rfkill_set_sw_state(asus->wlan.rfkill, blocked);
Corentin Chary279f8f92011-02-06 13:28:29 +0100666
Lukas Wunner125450f2018-09-08 09:59:01 +0200667 if (asus->hotplug_slot.ops) {
Corentin Charyafa7c882011-02-06 13:28:28 +0100668 bus = pci_find_bus(0, 1);
669 if (!bus) {
Joe Perches5ad77dc2011-03-29 15:21:35 -0700670 pr_warn("Unable to find PCI bus 1?\n");
Corentin Charyafa7c882011-02-06 13:28:28 +0100671 goto out_unlock;
672 }
673
674 if (pci_bus_read_config_dword(bus, 0, PCI_VENDOR_ID, &l)) {
675 pr_err("Unable to read PCI config space?\n");
676 goto out_unlock;
677 }
678 absent = (l == 0xffffffff);
679
680 if (blocked != absent) {
Joe Perches5ad77dc2011-03-29 15:21:35 -0700681 pr_warn("BIOS says wireless lan is %s, "
682 "but the pci device is %s\n",
683 blocked ? "blocked" : "unblocked",
684 absent ? "absent" : "present");
685 pr_warn("skipped wireless hotplug as probably "
686 "inappropriate for this model\n");
Corentin Charyafa7c882011-02-06 13:28:28 +0100687 goto out_unlock;
688 }
689
690 if (!blocked) {
691 dev = pci_get_slot(bus, 0);
692 if (dev) {
693 /* Device already present */
694 pci_dev_put(dev);
695 goto out_unlock;
696 }
697 dev = pci_scan_single_device(bus, 0);
698 if (dev) {
699 pci_bus_assign_resources(bus);
Yijing Wangc893d132014-05-30 11:01:03 +0800700 pci_bus_add_device(dev);
Corentin Charyafa7c882011-02-06 13:28:28 +0100701 }
702 } else {
703 dev = pci_get_slot(bus, 0);
704 if (dev) {
Yinghai Lu210647a2012-02-25 13:54:20 -0800705 pci_stop_and_remove_bus_device(dev);
Corentin Charyafa7c882011-02-06 13:28:28 +0100706 pci_dev_put(dev);
707 }
708 }
709 }
710
711out_unlock:
Rafael J. Wysocki8b9ec1d2014-01-10 15:27:08 +0100712 pci_unlock_rescan_remove();
Corentin Charye12e6d92011-02-26 10:20:31 +0100713 mutex_unlock(&asus->hotplug_lock);
Corentin Charyafa7c882011-02-06 13:28:28 +0100714}
715
Corentin Charye12e6d92011-02-26 10:20:31 +0100716static void asus_rfkill_notify(acpi_handle handle, u32 event, void *data)
Corentin Charyafa7c882011-02-06 13:28:28 +0100717{
Corentin Charye12e6d92011-02-26 10:20:31 +0100718 struct asus_wmi *asus = data;
Corentin Charyafa7c882011-02-06 13:28:28 +0100719
720 if (event != ACPI_NOTIFY_BUS_CHECK)
721 return;
722
Corentin Chary279f8f92011-02-06 13:28:29 +0100723 /*
Corentin Charye12e6d92011-02-26 10:20:31 +0100724 * We can't call directly asus_rfkill_hotplug because most
Corentin Chary279f8f92011-02-06 13:28:29 +0100725 * of the time WMBC is still being executed and not reetrant.
726 * There is currently no way to tell ACPICA that we want this
Corentin Charye12e6d92011-02-26 10:20:31 +0100727 * method to be serialized, we schedule a asus_rfkill_hotplug
Corentin Chary279f8f92011-02-06 13:28:29 +0100728 * call later, in a safer context.
729 */
Corentin Charye12e6d92011-02-26 10:20:31 +0100730 queue_work(asus->hotplug_workqueue, &asus->hotplug_work);
Corentin Charyafa7c882011-02-06 13:28:28 +0100731}
732
Corentin Charye12e6d92011-02-26 10:20:31 +0100733static int asus_register_rfkill_notifier(struct asus_wmi *asus, char *node)
Corentin Charyafa7c882011-02-06 13:28:28 +0100734{
735 acpi_status status;
736 acpi_handle handle;
737
738 status = acpi_get_handle(NULL, node, &handle);
739
740 if (ACPI_SUCCESS(status)) {
741 status = acpi_install_notify_handler(handle,
742 ACPI_SYSTEM_NOTIFY,
Corentin Charye12e6d92011-02-26 10:20:31 +0100743 asus_rfkill_notify, asus);
Corentin Charyafa7c882011-02-06 13:28:28 +0100744 if (ACPI_FAILURE(status))
Joe Perches5ad77dc2011-03-29 15:21:35 -0700745 pr_warn("Failed to register notify on %s\n", node);
Corentin Charyafa7c882011-02-06 13:28:28 +0100746 } else
747 return -ENODEV;
748
749 return 0;
750}
751
Corentin Charye12e6d92011-02-26 10:20:31 +0100752static void asus_unregister_rfkill_notifier(struct asus_wmi *asus, char *node)
Corentin Charyafa7c882011-02-06 13:28:28 +0100753{
754 acpi_status status = AE_OK;
755 acpi_handle handle;
756
757 status = acpi_get_handle(NULL, node, &handle);
758
759 if (ACPI_SUCCESS(status)) {
760 status = acpi_remove_notify_handler(handle,
Corentin Charye12e6d92011-02-26 10:20:31 +0100761 ACPI_SYSTEM_NOTIFY,
762 asus_rfkill_notify);
Corentin Charyafa7c882011-02-06 13:28:28 +0100763 if (ACPI_FAILURE(status))
764 pr_err("Error removing rfkill notify handler %s\n",
Corentin Charye12e6d92011-02-26 10:20:31 +0100765 node);
Corentin Charyafa7c882011-02-06 13:28:28 +0100766 }
767}
768
Corentin Charye12e6d92011-02-26 10:20:31 +0100769static int asus_get_adapter_status(struct hotplug_slot *hotplug_slot,
770 u8 *value)
Corentin Charyafa7c882011-02-06 13:28:28 +0100771{
Lukas Wunner125450f2018-09-08 09:59:01 +0200772 struct asus_wmi *asus = container_of(hotplug_slot,
773 struct asus_wmi, hotplug_slot);
Corentin Chary1d070f82011-02-26 10:20:36 +0100774 int result = asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_WLAN);
Corentin Charyafa7c882011-02-06 13:28:28 +0100775
Corentin Chary5c956382011-02-06 13:28:31 +0100776 if (result < 0)
777 return result;
Corentin Charyafa7c882011-02-06 13:28:28 +0100778
Corentin Chary5c956382011-02-06 13:28:31 +0100779 *value = !!result;
Corentin Charyafa7c882011-02-06 13:28:28 +0100780 return 0;
781}
782
Lukas Wunner81c4b5b2018-09-08 09:59:01 +0200783static const struct hotplug_slot_ops asus_hotplug_slot_ops = {
Corentin Charye12e6d92011-02-26 10:20:31 +0100784 .get_adapter_status = asus_get_adapter_status,
785 .get_power_status = asus_get_adapter_status,
Corentin Charyafa7c882011-02-06 13:28:28 +0100786};
787
Corentin Charye12e6d92011-02-26 10:20:31 +0100788static void asus_hotplug_work(struct work_struct *work)
Corentin Chary279f8f92011-02-06 13:28:29 +0100789{
Corentin Charye12e6d92011-02-26 10:20:31 +0100790 struct asus_wmi *asus;
Corentin Chary279f8f92011-02-06 13:28:29 +0100791
Corentin Charye12e6d92011-02-26 10:20:31 +0100792 asus = container_of(work, struct asus_wmi, hotplug_work);
793 asus_rfkill_hotplug(asus);
Corentin Chary279f8f92011-02-06 13:28:29 +0100794}
795
Corentin Charye12e6d92011-02-26 10:20:31 +0100796static int asus_setup_pci_hotplug(struct asus_wmi *asus)
Corentin Charyafa7c882011-02-06 13:28:28 +0100797{
798 int ret = -ENOMEM;
799 struct pci_bus *bus = pci_find_bus(0, 1);
800
801 if (!bus) {
802 pr_err("Unable to find wifi PCI bus\n");
803 return -ENODEV;
804 }
805
Corentin Charye12e6d92011-02-26 10:20:31 +0100806 asus->hotplug_workqueue =
807 create_singlethread_workqueue("hotplug_workqueue");
808 if (!asus->hotplug_workqueue)
Corentin Chary279f8f92011-02-06 13:28:29 +0100809 goto error_workqueue;
810
Corentin Charye12e6d92011-02-26 10:20:31 +0100811 INIT_WORK(&asus->hotplug_work, asus_hotplug_work);
Corentin Chary279f8f92011-02-06 13:28:29 +0100812
Lukas Wunner125450f2018-09-08 09:59:01 +0200813 asus->hotplug_slot.ops = &asus_hotplug_slot_ops;
Corentin Charyafa7c882011-02-06 13:28:28 +0100814
Lukas Wunner125450f2018-09-08 09:59:01 +0200815 ret = pci_hp_register(&asus->hotplug_slot, bus, 0, "asus-wifi");
Corentin Charyafa7c882011-02-06 13:28:28 +0100816 if (ret) {
817 pr_err("Unable to register hotplug slot - %d\n", ret);
818 goto error_register;
819 }
820
821 return 0;
822
823error_register:
Lukas Wunner125450f2018-09-08 09:59:01 +0200824 asus->hotplug_slot.ops = NULL;
Corentin Charye12e6d92011-02-26 10:20:31 +0100825 destroy_workqueue(asus->hotplug_workqueue);
Corentin Chary279f8f92011-02-06 13:28:29 +0100826error_workqueue:
Corentin Charyafa7c882011-02-06 13:28:28 +0100827 return ret;
828}
829
830/*
Corentin Charyba48fdb2010-11-29 08:14:07 +0100831 * Rfkill devices
832 */
Corentin Charye12e6d92011-02-26 10:20:31 +0100833static int asus_rfkill_set(void *data, bool blocked)
Corentin Charyba48fdb2010-11-29 08:14:07 +0100834{
Corentin Charya7ce3f02011-02-26 10:20:33 +0100835 struct asus_rfkill *priv = data;
Corentin Charyba48fdb2010-11-29 08:14:07 +0100836 u32 ctrl_param = !blocked;
AceLan Kaoa50bd122012-07-26 17:13:31 +0800837 u32 dev_id = priv->dev_id;
Corentin Charyba48fdb2010-11-29 08:14:07 +0100838
AceLan Kaoa50bd122012-07-26 17:13:31 +0800839 /*
840 * If the user bit is set, BIOS can't set and record the wlan status,
841 * it will report the value read from id ASUS_WMI_DEVID_WLAN_LED
842 * while we query the wlan status through WMI(ASUS_WMI_DEVID_WLAN).
843 * So, we have to record wlan status in id ASUS_WMI_DEVID_WLAN_LED
844 * while setting the wlan status through WMI.
845 * This is also the behavior that windows app will do.
846 */
847 if ((dev_id == ASUS_WMI_DEVID_WLAN) &&
848 priv->asus->driver->wlan_ctrl_by_user)
849 dev_id = ASUS_WMI_DEVID_WLAN_LED;
850
851 return asus_wmi_set_devstate(dev_id, ctrl_param, NULL);
Corentin Charyba48fdb2010-11-29 08:14:07 +0100852}
853
Corentin Charye12e6d92011-02-26 10:20:31 +0100854static void asus_rfkill_query(struct rfkill *rfkill, void *data)
Corentin Charyba48fdb2010-11-29 08:14:07 +0100855{
Corentin Charya7ce3f02011-02-26 10:20:33 +0100856 struct asus_rfkill *priv = data;
Corentin Chary5c956382011-02-06 13:28:31 +0100857 int result;
Corentin Charyba48fdb2010-11-29 08:14:07 +0100858
Corentin Chary1d070f82011-02-26 10:20:36 +0100859 result = asus_wmi_get_devstate_simple(priv->asus, priv->dev_id);
Corentin Charyba48fdb2010-11-29 08:14:07 +0100860
Corentin Chary5c956382011-02-06 13:28:31 +0100861 if (result < 0)
Corentin Charye12e6d92011-02-26 10:20:31 +0100862 return;
Corentin Charyba48fdb2010-11-29 08:14:07 +0100863
Corentin Charya7ce3f02011-02-26 10:20:33 +0100864 rfkill_set_sw_state(priv->rfkill, !result);
Corentin Charyba48fdb2010-11-29 08:14:07 +0100865}
866
Corentin Charye12e6d92011-02-26 10:20:31 +0100867static int asus_rfkill_wlan_set(void *data, bool blocked)
Corentin Chary279f8f92011-02-06 13:28:29 +0100868{
Corentin Charya7ce3f02011-02-26 10:20:33 +0100869 struct asus_rfkill *priv = data;
870 struct asus_wmi *asus = priv->asus;
Corentin Chary279f8f92011-02-06 13:28:29 +0100871 int ret;
872
873 /*
874 * This handler is enabled only if hotplug is enabled.
Corentin Charye12e6d92011-02-26 10:20:31 +0100875 * In this case, the asus_wmi_set_devstate() will
Corentin Chary279f8f92011-02-06 13:28:29 +0100876 * trigger a wmi notification and we need to wait
877 * this call to finish before being able to call
878 * any wmi method
879 */
Corentin Charye12e6d92011-02-26 10:20:31 +0100880 mutex_lock(&asus->wmi_lock);
Corentin Charya7ce3f02011-02-26 10:20:33 +0100881 ret = asus_rfkill_set(data, blocked);
Corentin Charye12e6d92011-02-26 10:20:31 +0100882 mutex_unlock(&asus->wmi_lock);
Corentin Chary279f8f92011-02-06 13:28:29 +0100883 return ret;
884}
885
Corentin Charye12e6d92011-02-26 10:20:31 +0100886static const struct rfkill_ops asus_rfkill_wlan_ops = {
887 .set_block = asus_rfkill_wlan_set,
Corentin Charya7ce3f02011-02-26 10:20:33 +0100888 .query = asus_rfkill_query,
Corentin Chary279f8f92011-02-06 13:28:29 +0100889};
890
Corentin Charye12e6d92011-02-26 10:20:31 +0100891static const struct rfkill_ops asus_rfkill_ops = {
892 .set_block = asus_rfkill_set,
893 .query = asus_rfkill_query,
Corentin Charyba48fdb2010-11-29 08:14:07 +0100894};
895
Corentin Charye12e6d92011-02-26 10:20:31 +0100896static int asus_new_rfkill(struct asus_wmi *asus,
Corentin Charya7ce3f02011-02-26 10:20:33 +0100897 struct asus_rfkill *arfkill,
Corentin Charye12e6d92011-02-26 10:20:31 +0100898 const char *name, enum rfkill_type type, int dev_id)
Corentin Charyba48fdb2010-11-29 08:14:07 +0100899{
Corentin Chary1d070f82011-02-26 10:20:36 +0100900 int result = asus_wmi_get_devstate_simple(asus, dev_id);
Corentin Charya7ce3f02011-02-26 10:20:33 +0100901 struct rfkill **rfkill = &arfkill->rfkill;
Corentin Charyba48fdb2010-11-29 08:14:07 +0100902
Corentin Chary5c956382011-02-06 13:28:31 +0100903 if (result < 0)
904 return result;
Corentin Charyba48fdb2010-11-29 08:14:07 +0100905
Corentin Charya7ce3f02011-02-26 10:20:33 +0100906 arfkill->dev_id = dev_id;
907 arfkill->asus = asus;
908
AceLan Kaoc87992d2012-03-20 09:53:08 +0100909 if (dev_id == ASUS_WMI_DEVID_WLAN &&
910 asus->driver->quirks->hotplug_wireless)
Corentin Charye12e6d92011-02-26 10:20:31 +0100911 *rfkill = rfkill_alloc(name, &asus->platform_device->dev, type,
Corentin Charya7ce3f02011-02-26 10:20:33 +0100912 &asus_rfkill_wlan_ops, arfkill);
Corentin Chary279f8f92011-02-06 13:28:29 +0100913 else
Corentin Charye12e6d92011-02-26 10:20:31 +0100914 *rfkill = rfkill_alloc(name, &asus->platform_device->dev, type,
Corentin Charya7ce3f02011-02-26 10:20:33 +0100915 &asus_rfkill_ops, arfkill);
Corentin Charyba48fdb2010-11-29 08:14:07 +0100916
917 if (!*rfkill)
918 return -EINVAL;
919
AceLan Kaoe8f56c82013-05-30 10:31:50 +0800920 if ((dev_id == ASUS_WMI_DEVID_WLAN) &&
AceLan Kaof5156232014-07-09 16:18:18 +0800921 (asus->driver->quirks->wapf > 0))
AceLan Kao6cae06e2012-07-27 16:51:59 +0800922 rfkill_set_led_trigger_name(*rfkill, "asus-wlan");
923
Corentin Chary5c956382011-02-06 13:28:31 +0100924 rfkill_init_sw_state(*rfkill, !result);
Corentin Charyba48fdb2010-11-29 08:14:07 +0100925 result = rfkill_register(*rfkill);
926 if (result) {
927 rfkill_destroy(*rfkill);
928 *rfkill = NULL;
929 return result;
930 }
931 return 0;
932}
933
Corentin Charye12e6d92011-02-26 10:20:31 +0100934static void asus_wmi_rfkill_exit(struct asus_wmi *asus)
Corentin Charyba48fdb2010-11-29 08:14:07 +0100935{
João Paulo Rechi Vitacf48bf92018-05-22 14:30:15 -0700936 if (asus->driver->wlan_ctrl_by_user && ashs_present())
937 return;
938
Corentin Charye12e6d92011-02-26 10:20:31 +0100939 asus_unregister_rfkill_notifier(asus, "\\_SB.PCI0.P0P5");
940 asus_unregister_rfkill_notifier(asus, "\\_SB.PCI0.P0P6");
941 asus_unregister_rfkill_notifier(asus, "\\_SB.PCI0.P0P7");
Corentin Charya7ce3f02011-02-26 10:20:33 +0100942 if (asus->wlan.rfkill) {
943 rfkill_unregister(asus->wlan.rfkill);
944 rfkill_destroy(asus->wlan.rfkill);
945 asus->wlan.rfkill = NULL;
Corentin Charyba48fdb2010-11-29 08:14:07 +0100946 }
Corentin Charyafa7c882011-02-06 13:28:28 +0100947 /*
948 * Refresh pci hotplug in case the rfkill state was changed after
Corentin Charye12e6d92011-02-26 10:20:31 +0100949 * asus_unregister_rfkill_notifier()
Corentin Charyafa7c882011-02-06 13:28:28 +0100950 */
Corentin Charye12e6d92011-02-26 10:20:31 +0100951 asus_rfkill_hotplug(asus);
Lukas Wunner125450f2018-09-08 09:59:01 +0200952 if (asus->hotplug_slot.ops)
953 pci_hp_deregister(&asus->hotplug_slot);
Corentin Charye12e6d92011-02-26 10:20:31 +0100954 if (asus->hotplug_workqueue)
955 destroy_workqueue(asus->hotplug_workqueue);
Corentin Charyafa7c882011-02-06 13:28:28 +0100956
Corentin Charya7ce3f02011-02-26 10:20:33 +0100957 if (asus->bluetooth.rfkill) {
958 rfkill_unregister(asus->bluetooth.rfkill);
959 rfkill_destroy(asus->bluetooth.rfkill);
960 asus->bluetooth.rfkill = NULL;
Corentin Charyba48fdb2010-11-29 08:14:07 +0100961 }
Corentin Charya7ce3f02011-02-26 10:20:33 +0100962 if (asus->wimax.rfkill) {
963 rfkill_unregister(asus->wimax.rfkill);
964 rfkill_destroy(asus->wimax.rfkill);
965 asus->wimax.rfkill = NULL;
Corentin Chary2e9e1592011-02-06 13:28:37 +0100966 }
Corentin Charya7ce3f02011-02-26 10:20:33 +0100967 if (asus->wwan3g.rfkill) {
968 rfkill_unregister(asus->wwan3g.rfkill);
969 rfkill_destroy(asus->wwan3g.rfkill);
970 asus->wwan3g.rfkill = NULL;
Corentin Charyba48fdb2010-11-29 08:14:07 +0100971 }
Corentin Chary43be8bd2011-07-01 11:34:40 +0200972 if (asus->gps.rfkill) {
973 rfkill_unregister(asus->gps.rfkill);
974 rfkill_destroy(asus->gps.rfkill);
975 asus->gps.rfkill = NULL;
976 }
Corentin Charya912d322011-07-01 11:34:41 +0200977 if (asus->uwb.rfkill) {
978 rfkill_unregister(asus->uwb.rfkill);
979 rfkill_destroy(asus->uwb.rfkill);
980 asus->uwb.rfkill = NULL;
981 }
Corentin Charyba48fdb2010-11-29 08:14:07 +0100982}
983
Corentin Charye12e6d92011-02-26 10:20:31 +0100984static int asus_wmi_rfkill_init(struct asus_wmi *asus)
Corentin Charyba48fdb2010-11-29 08:14:07 +0100985{
986 int result = 0;
987
Corentin Charye12e6d92011-02-26 10:20:31 +0100988 mutex_init(&asus->hotplug_lock);
989 mutex_init(&asus->wmi_lock);
Corentin Charyafa7c882011-02-06 13:28:28 +0100990
Corentin Charya7ce3f02011-02-26 10:20:33 +0100991 result = asus_new_rfkill(asus, &asus->wlan, "asus-wlan",
992 RFKILL_TYPE_WLAN, ASUS_WMI_DEVID_WLAN);
Corentin Charyba48fdb2010-11-29 08:14:07 +0100993
994 if (result && result != -ENODEV)
995 goto exit;
996
Corentin Charya7ce3f02011-02-26 10:20:33 +0100997 result = asus_new_rfkill(asus, &asus->bluetooth,
Corentin Charye12e6d92011-02-26 10:20:31 +0100998 "asus-bluetooth", RFKILL_TYPE_BLUETOOTH,
999 ASUS_WMI_DEVID_BLUETOOTH);
Corentin Charyba48fdb2010-11-29 08:14:07 +01001000
1001 if (result && result != -ENODEV)
1002 goto exit;
1003
Corentin Charya7ce3f02011-02-26 10:20:33 +01001004 result = asus_new_rfkill(asus, &asus->wimax, "asus-wimax",
1005 RFKILL_TYPE_WIMAX, ASUS_WMI_DEVID_WIMAX);
Corentin Chary2e9e1592011-02-06 13:28:37 +01001006
1007 if (result && result != -ENODEV)
1008 goto exit;
1009
Corentin Charya7ce3f02011-02-26 10:20:33 +01001010 result = asus_new_rfkill(asus, &asus->wwan3g, "asus-wwan3g",
1011 RFKILL_TYPE_WWAN, ASUS_WMI_DEVID_WWAN3G);
Corentin Charyba48fdb2010-11-29 08:14:07 +01001012
1013 if (result && result != -ENODEV)
1014 goto exit;
1015
Corentin Chary43be8bd2011-07-01 11:34:40 +02001016 result = asus_new_rfkill(asus, &asus->gps, "asus-gps",
1017 RFKILL_TYPE_GPS, ASUS_WMI_DEVID_GPS);
1018
1019 if (result && result != -ENODEV)
1020 goto exit;
1021
Corentin Charya912d322011-07-01 11:34:41 +02001022 result = asus_new_rfkill(asus, &asus->uwb, "asus-uwb",
1023 RFKILL_TYPE_UWB, ASUS_WMI_DEVID_UWB);
1024
1025 if (result && result != -ENODEV)
1026 goto exit;
1027
AceLan Kaoc87992d2012-03-20 09:53:08 +01001028 if (!asus->driver->quirks->hotplug_wireless)
Corentin Charyc14d4b82011-02-06 13:28:40 +01001029 goto exit;
1030
Corentin Charye12e6d92011-02-26 10:20:31 +01001031 result = asus_setup_pci_hotplug(asus);
Corentin Charyafa7c882011-02-06 13:28:28 +01001032 /*
1033 * If we get -EBUSY then something else is handling the PCI hotplug -
1034 * don't fail in this case
1035 */
1036 if (result == -EBUSY)
1037 result = 0;
1038
Corentin Charye12e6d92011-02-26 10:20:31 +01001039 asus_register_rfkill_notifier(asus, "\\_SB.PCI0.P0P5");
1040 asus_register_rfkill_notifier(asus, "\\_SB.PCI0.P0P6");
1041 asus_register_rfkill_notifier(asus, "\\_SB.PCI0.P0P7");
Corentin Charyafa7c882011-02-06 13:28:28 +01001042 /*
1043 * Refresh pci hotplug in case the rfkill state was changed during
1044 * setup.
1045 */
Corentin Charye12e6d92011-02-26 10:20:31 +01001046 asus_rfkill_hotplug(asus);
Corentin Charyafa7c882011-02-06 13:28:28 +01001047
Corentin Charyba48fdb2010-11-29 08:14:07 +01001048exit:
1049 if (result && result != -ENODEV)
Corentin Charye12e6d92011-02-26 10:20:31 +01001050 asus_wmi_rfkill_exit(asus);
Corentin Charyba48fdb2010-11-29 08:14:07 +01001051
1052 if (result == -ENODEV)
1053 result = 0;
1054
1055 return result;
1056}
1057
Kai-Chuan Hsieh8023eff2016-09-01 23:55:55 +08001058static void asus_wmi_set_xusb2pr(struct asus_wmi *asus)
1059{
1060 struct pci_dev *xhci_pdev;
1061 u32 orig_ports_available;
1062 u32 ports_available = asus->driver->quirks->xusb2pr;
1063
1064 xhci_pdev = pci_get_device(PCI_VENDOR_ID_INTEL,
1065 PCI_DEVICE_ID_INTEL_LYNXPOINT_LP_XHCI,
1066 NULL);
1067
1068 if (!xhci_pdev)
1069 return;
1070
1071 pci_read_config_dword(xhci_pdev, USB_INTEL_XUSB2PR,
1072 &orig_ports_available);
1073
1074 pci_write_config_dword(xhci_pdev, USB_INTEL_XUSB2PR,
1075 cpu_to_le32(ports_available));
1076
1077 pr_info("set USB_INTEL_XUSB2PR old: 0x%04x, new: 0x%04x\n",
1078 orig_ports_available, ports_available);
1079}
1080
Corentin Charyba48fdb2010-11-29 08:14:07 +01001081/*
Oleksij Rempele9b61512017-04-28 16:19:49 +02001082 * Some devices dont support or have borcken get_als method
1083 * but still support set method.
1084 */
1085static void asus_wmi_set_als(void)
1086{
1087 asus_wmi_set_devstate(ASUS_WMI_DEVID_ALS_ENABLE, 1, NULL);
1088}
1089
1090/*
Corentin Charye07babd2011-02-26 10:20:42 +01001091 * Hwmon device
1092 */
Kast Bernd53e755c2015-05-13 16:24:16 +02001093static int asus_hwmon_agfn_fan_speed_read(struct asus_wmi *asus, int fan,
1094 int *speed)
1095{
1096 struct fan_args args = {
1097 .agfn.len = sizeof(args),
1098 .agfn.mfun = ASUS_FAN_MFUN,
1099 .agfn.sfun = ASUS_FAN_SFUN_READ,
1100 .fan = fan,
1101 .speed = 0,
1102 };
1103 struct acpi_buffer input = { (acpi_size) sizeof(args), &args };
1104 int status;
1105
1106 if (fan != 1)
1107 return -EINVAL;
1108
1109 status = asus_wmi_evaluate_method_agfn(input);
1110
1111 if (status || args.agfn.err)
1112 return -ENXIO;
1113
1114 if (speed)
1115 *speed = args.speed;
1116
1117 return 0;
1118}
1119
1120static int asus_hwmon_agfn_fan_speed_write(struct asus_wmi *asus, int fan,
1121 int *speed)
1122{
1123 struct fan_args args = {
1124 .agfn.len = sizeof(args),
1125 .agfn.mfun = ASUS_FAN_MFUN,
1126 .agfn.sfun = ASUS_FAN_SFUN_WRITE,
1127 .fan = fan,
1128 .speed = speed ? *speed : 0,
1129 };
1130 struct acpi_buffer input = { (acpi_size) sizeof(args), &args };
1131 int status;
1132
1133 /* 1: for setting 1st fan's speed 0: setting auto mode */
1134 if (fan != 1 && fan != 0)
1135 return -EINVAL;
1136
1137 status = asus_wmi_evaluate_method_agfn(input);
1138
1139 if (status || args.agfn.err)
1140 return -ENXIO;
1141
1142 if (speed && fan == 1)
1143 asus->asus_hwmon_pwm = *speed;
1144
1145 return 0;
1146}
1147
1148/*
1149 * Check if we can read the speed of one fan. If true we assume we can also
1150 * control it.
1151 */
1152static int asus_hwmon_get_fan_number(struct asus_wmi *asus, int *num_fans)
1153{
1154 int status;
1155 int speed = 0;
1156
1157 *num_fans = 0;
1158
1159 status = asus_hwmon_agfn_fan_speed_read(asus, 1, &speed);
1160 if (!status)
1161 *num_fans = 1;
1162
1163 return 0;
1164}
1165
1166static int asus_hwmon_fan_set_auto(struct asus_wmi *asus)
1167{
1168 int status;
1169
1170 status = asus_hwmon_agfn_fan_speed_write(asus, 0, NULL);
1171 if (status)
1172 return -ENXIO;
1173
1174 asus->asus_hwmon_fan_manual_mode = false;
1175
1176 return 0;
1177}
1178
1179static int asus_hwmon_fan_rpm_show(struct device *dev, int fan)
1180{
1181 struct asus_wmi *asus = dev_get_drvdata(dev);
1182 int value;
1183 int ret;
1184
1185 /* no speed readable on manual mode */
1186 if (asus->asus_hwmon_fan_manual_mode)
1187 return -ENXIO;
1188
1189 ret = asus_hwmon_agfn_fan_speed_read(asus, fan+1, &value);
1190 if (ret) {
1191 pr_warn("reading fan speed failed: %d\n", ret);
1192 return -ENXIO;
1193 }
1194
1195 return value;
1196}
1197
1198static void asus_hwmon_pwm_show(struct asus_wmi *asus, int fan, int *value)
1199{
1200 int err;
1201
1202 if (asus->asus_hwmon_pwm >= 0) {
1203 *value = asus->asus_hwmon_pwm;
1204 return;
1205 }
1206
1207 err = asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_FAN_CTRL, value);
1208 if (err < 0)
1209 return;
1210
1211 *value &= 0xFF;
1212
1213 if (*value == 1) /* Low Speed */
1214 *value = 85;
1215 else if (*value == 2)
1216 *value = 170;
1217 else if (*value == 3)
1218 *value = 255;
1219 else if (*value) {
1220 pr_err("Unknown fan speed %#x\n", *value);
1221 *value = -1;
1222 }
1223}
1224
1225static ssize_t pwm1_show(struct device *dev,
Corentin Chary49979d02011-07-01 11:34:26 +02001226 struct device_attribute *attr,
1227 char *buf)
Corentin Charye07babd2011-02-26 10:20:42 +01001228{
1229 struct asus_wmi *asus = dev_get_drvdata(dev);
Kast Bernd53e755c2015-05-13 16:24:16 +02001230 int value;
Corentin Charye07babd2011-02-26 10:20:42 +01001231
Kast Bernd53e755c2015-05-13 16:24:16 +02001232 asus_hwmon_pwm_show(asus, 0, &value);
Corentin Charye07babd2011-02-26 10:20:42 +01001233
1234 return sprintf(buf, "%d\n", value);
1235}
1236
Kast Bernd53e755c2015-05-13 16:24:16 +02001237static ssize_t pwm1_store(struct device *dev,
1238 struct device_attribute *attr,
1239 const char *buf, size_t count) {
1240 struct asus_wmi *asus = dev_get_drvdata(dev);
1241 int value;
1242 int state;
1243 int ret;
1244
1245 ret = kstrtouint(buf, 10, &value);
1246
1247 if (ret)
1248 return ret;
1249
1250 value = clamp(value, 0, 255);
1251
1252 state = asus_hwmon_agfn_fan_speed_write(asus, 1, &value);
1253 if (state)
1254 pr_warn("Setting fan speed failed: %d\n", state);
1255 else
1256 asus->asus_hwmon_fan_manual_mode = true;
1257
1258 return count;
1259}
1260
1261static ssize_t fan1_input_show(struct device *dev,
1262 struct device_attribute *attr,
1263 char *buf)
1264{
1265 int value = asus_hwmon_fan_rpm_show(dev, 0);
1266
1267 return sprintf(buf, "%d\n", value < 0 ? -1 : value*100);
1268
1269}
1270
1271static ssize_t pwm1_enable_show(struct device *dev,
1272 struct device_attribute *attr,
1273 char *buf)
1274{
1275 struct asus_wmi *asus = dev_get_drvdata(dev);
1276
1277 if (asus->asus_hwmon_fan_manual_mode)
1278 return sprintf(buf, "%d\n", ASUS_FAN_CTRL_MANUAL);
1279
1280 return sprintf(buf, "%d\n", ASUS_FAN_CTRL_AUTO);
1281}
1282
1283static ssize_t pwm1_enable_store(struct device *dev,
1284 struct device_attribute *attr,
1285 const char *buf, size_t count)
1286{
1287 struct asus_wmi *asus = dev_get_drvdata(dev);
1288 int status = 0;
1289 int state;
1290 int ret;
1291
1292 ret = kstrtouint(buf, 10, &state);
1293
1294 if (ret)
1295 return ret;
1296
1297 if (state == ASUS_FAN_CTRL_MANUAL)
1298 asus->asus_hwmon_fan_manual_mode = true;
1299 else
1300 status = asus_hwmon_fan_set_auto(asus);
1301
1302 if (status)
1303 return status;
1304
1305 return count;
1306}
1307
1308static ssize_t fan1_label_show(struct device *dev,
1309 struct device_attribute *attr,
1310 char *buf)
1311{
1312 return sprintf(buf, "%s\n", ASUS_FAN_DESC);
1313}
1314
Corentin Chary6118b8a2011-07-01 11:34:36 +02001315static ssize_t asus_hwmon_temp1(struct device *dev,
1316 struct device_attribute *attr,
1317 char *buf)
1318{
1319 struct asus_wmi *asus = dev_get_drvdata(dev);
1320 u32 value;
1321 int err;
1322
1323 err = asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_THERMAL_CTRL, &value);
1324
1325 if (err < 0)
1326 return err;
1327
Rasmus Villemoese866a2e2015-10-01 23:45:31 +02001328 value = DECI_KELVIN_TO_CELSIUS((value & 0xFFFF)) * 1000;
Corentin Chary6118b8a2011-07-01 11:34:36 +02001329
1330 return sprintf(buf, "%d\n", value);
1331}
1332
Kast Bernd53e755c2015-05-13 16:24:16 +02001333/* Fan1 */
1334static DEVICE_ATTR_RW(pwm1);
1335static DEVICE_ATTR_RW(pwm1_enable);
1336static DEVICE_ATTR_RO(fan1_input);
1337static DEVICE_ATTR_RO(fan1_label);
1338
1339/* Temperature */
Guenter Roeck50a639f2013-11-23 11:03:17 -08001340static DEVICE_ATTR(temp1_input, S_IRUGO, asus_hwmon_temp1, NULL);
Corentin Charye07babd2011-02-26 10:20:42 +01001341
1342static struct attribute *hwmon_attributes[] = {
Guenter Roeck50a639f2013-11-23 11:03:17 -08001343 &dev_attr_pwm1.attr,
Kast Bernd53e755c2015-05-13 16:24:16 +02001344 &dev_attr_pwm1_enable.attr,
1345 &dev_attr_fan1_input.attr,
1346 &dev_attr_fan1_label.attr,
1347
Guenter Roeck50a639f2013-11-23 11:03:17 -08001348 &dev_attr_temp1_input.attr,
Corentin Charye07babd2011-02-26 10:20:42 +01001349 NULL
1350};
1351
Al Viro587a1f12011-07-23 23:11:19 -04001352static umode_t asus_hwmon_sysfs_is_visible(struct kobject *kobj,
Corentin Charye02431d2011-07-01 11:34:37 +02001353 struct attribute *attr, int idx)
Corentin Charye07babd2011-02-26 10:20:42 +01001354{
1355 struct device *dev = container_of(kobj, struct device, kobj);
1356 struct platform_device *pdev = to_platform_device(dev->parent);
1357 struct asus_wmi *asus = platform_get_drvdata(pdev);
Corentin Charye07babd2011-02-26 10:20:42 +01001358 int dev_id = -1;
Kast Bernd53e755c2015-05-13 16:24:16 +02001359 int fan_attr = -1;
Corentin Charye07babd2011-02-26 10:20:42 +01001360 u32 value = ASUS_WMI_UNSUPPORTED_METHOD;
Kast Bernd53e755c2015-05-13 16:24:16 +02001361 bool ok = true;
Corentin Charye07babd2011-02-26 10:20:42 +01001362
Guenter Roeck50a639f2013-11-23 11:03:17 -08001363 if (attr == &dev_attr_pwm1.attr)
Corentin Charye07babd2011-02-26 10:20:42 +01001364 dev_id = ASUS_WMI_DEVID_FAN_CTRL;
Guenter Roeck50a639f2013-11-23 11:03:17 -08001365 else if (attr == &dev_attr_temp1_input.attr)
Corentin Charye02431d2011-07-01 11:34:37 +02001366 dev_id = ASUS_WMI_DEVID_THERMAL_CTRL;
Corentin Charye07babd2011-02-26 10:20:42 +01001367
Kast Bernd53e755c2015-05-13 16:24:16 +02001368
1369 if (attr == &dev_attr_fan1_input.attr
1370 || attr == &dev_attr_fan1_label.attr
1371 || attr == &dev_attr_pwm1.attr
1372 || attr == &dev_attr_pwm1_enable.attr) {
1373 fan_attr = 1;
1374 }
1375
Corentin Charye07babd2011-02-26 10:20:42 +01001376 if (dev_id != -1) {
1377 int err = asus_wmi_get_devstate(asus, dev_id, &value);
1378
Kast Bernd53e755c2015-05-13 16:24:16 +02001379 if (err < 0 && fan_attr == -1)
Al Viroe772aed2011-07-23 20:59:40 -04001380 return 0; /* can't return negative here */
Corentin Charye07babd2011-02-26 10:20:42 +01001381 }
1382
1383 if (dev_id == ASUS_WMI_DEVID_FAN_CTRL) {
1384 /*
1385 * We need to find a better way, probably using sfun,
1386 * bits or spec ...
1387 * Currently we disable it if:
1388 * - ASUS_WMI_UNSUPPORTED_METHOD is returned
1389 * - reverved bits are non-zero
1390 * - sfun and presence bit are not set
1391 */
Corentin Chary49979d02011-07-01 11:34:26 +02001392 if (value == ASUS_WMI_UNSUPPORTED_METHOD || value & 0xFFF80000
Corentin Charye07babd2011-02-26 10:20:42 +01001393 || (!asus->sfun && !(value & ASUS_WMI_DSTS_PRESENCE_BIT)))
1394 ok = false;
Kast Bernd53e755c2015-05-13 16:24:16 +02001395 else
1396 ok = fan_attr <= asus->asus_hwmon_num_fans;
Corentin Charye02431d2011-07-01 11:34:37 +02001397 } else if (dev_id == ASUS_WMI_DEVID_THERMAL_CTRL) {
1398 /* If value is zero, something is clearly wrong */
Kast Bernd53e755c2015-05-13 16:24:16 +02001399 if (!value)
Corentin Charye02431d2011-07-01 11:34:37 +02001400 ok = false;
Kast Bernd53e755c2015-05-13 16:24:16 +02001401 } else if (fan_attr <= asus->asus_hwmon_num_fans && fan_attr != -1) {
1402 ok = true;
1403 } else {
1404 ok = false;
Corentin Charye07babd2011-02-26 10:20:42 +01001405 }
1406
1407 return ok ? attr->mode : 0;
1408}
1409
Arvind Yadave90d9ba2017-07-11 16:18:19 +05301410static const struct attribute_group hwmon_attribute_group = {
Corentin Charye07babd2011-02-26 10:20:42 +01001411 .is_visible = asus_hwmon_sysfs_is_visible,
1412 .attrs = hwmon_attributes
1413};
Guenter Roeck50a639f2013-11-23 11:03:17 -08001414__ATTRIBUTE_GROUPS(hwmon_attribute);
Corentin Charye07babd2011-02-26 10:20:42 +01001415
1416static int asus_wmi_hwmon_init(struct asus_wmi *asus)
1417{
1418 struct device *hwmon;
Corentin Charye07babd2011-02-26 10:20:42 +01001419
Guenter Roeck50a639f2013-11-23 11:03:17 -08001420 hwmon = hwmon_device_register_with_groups(&asus->platform_device->dev,
1421 "asus", asus,
1422 hwmon_attribute_groups);
Corentin Charye07babd2011-02-26 10:20:42 +01001423 if (IS_ERR(hwmon)) {
1424 pr_err("Could not register asus hwmon device\n");
1425 return PTR_ERR(hwmon);
1426 }
Guenter Roeck50a639f2013-11-23 11:03:17 -08001427 return 0;
Corentin Charye07babd2011-02-26 10:20:42 +01001428}
1429
1430/*
Corentin Chary084fca62010-11-29 08:14:06 +01001431 * Backlight
1432 */
Corentin Chary1d070f82011-02-26 10:20:36 +01001433static int read_backlight_power(struct asus_wmi *asus)
Corentin Charyb7187262011-02-06 13:28:39 +01001434{
AceLan Kao6e0044b2012-03-20 09:53:09 +01001435 int ret;
1436 if (asus->driver->quirks->store_backlight_power)
1437 ret = !asus->driver->panel_power;
1438 else
1439 ret = asus_wmi_get_devstate_simple(asus,
1440 ASUS_WMI_DEVID_BACKLIGHT);
Corentin Charyb7187262011-02-06 13:28:39 +01001441
1442 if (ret < 0)
1443 return ret;
1444
1445 return ret ? FB_BLANK_UNBLANK : FB_BLANK_POWERDOWN;
1446}
1447
Corentin Chary8fbea012011-02-26 10:20:37 +01001448static int read_brightness_max(struct asus_wmi *asus)
1449{
1450 u32 retval;
1451 int err;
1452
1453 err = asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_BRIGHTNESS, &retval);
1454
1455 if (err < 0)
1456 return err;
1457
1458 retval = retval & ASUS_WMI_DSTS_MAX_BRIGTH_MASK;
1459 retval >>= 8;
1460
1461 if (!retval)
1462 return -ENODEV;
1463
1464 return retval;
1465}
1466
Yong Wang3d7b1652010-04-11 09:27:54 +08001467static int read_brightness(struct backlight_device *bd)
1468{
Corentin Chary1d070f82011-02-26 10:20:36 +01001469 struct asus_wmi *asus = bl_get_data(bd);
Dan Carpenter0986f252011-03-15 10:06:23 +03001470 u32 retval;
1471 int err;
Yong Wang3d7b1652010-04-11 09:27:54 +08001472
Corentin Chary1d070f82011-02-26 10:20:36 +01001473 err = asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_BRIGHTNESS, &retval);
Yong Wang3d7b1652010-04-11 09:27:54 +08001474
Corentin Charyd33da3b2011-02-26 10:20:35 +01001475 if (err < 0)
1476 return err;
1477
1478 return retval & ASUS_WMI_DSTS_BRIGHTNESS_MASK;
Yong Wang3d7b1652010-04-11 09:27:54 +08001479}
1480
AceLan Kaoc87992d2012-03-20 09:53:08 +01001481static u32 get_scalar_command(struct backlight_device *bd)
1482{
1483 struct asus_wmi *asus = bl_get_data(bd);
1484 u32 ctrl_param = 0;
1485
1486 if ((asus->driver->brightness < bd->props.brightness) ||
1487 bd->props.brightness == bd->props.max_brightness)
1488 ctrl_param = 0x00008001;
1489 else if ((asus->driver->brightness > bd->props.brightness) ||
1490 bd->props.brightness == 0)
1491 ctrl_param = 0x00008000;
1492
1493 asus->driver->brightness = bd->props.brightness;
1494
1495 return ctrl_param;
1496}
1497
Yong Wang3d7b1652010-04-11 09:27:54 +08001498static int update_bl_status(struct backlight_device *bd)
1499{
Corentin Chary1d070f82011-02-26 10:20:36 +01001500 struct asus_wmi *asus = bl_get_data(bd);
Corentin Charydfed65d2010-11-29 08:14:12 +01001501 u32 ctrl_param;
AceLan Kao6e0044b2012-03-20 09:53:09 +01001502 int power, err = 0;
Corentin Charyb7187262011-02-06 13:28:39 +01001503
Corentin Chary1d070f82011-02-26 10:20:36 +01001504 power = read_backlight_power(asus);
Corentin Charyb7187262011-02-06 13:28:39 +01001505 if (power != -ENODEV && bd->props.power != power) {
1506 ctrl_param = !!(bd->props.power == FB_BLANK_UNBLANK);
Corentin Charyd33da3b2011-02-26 10:20:35 +01001507 err = asus_wmi_set_devstate(ASUS_WMI_DEVID_BACKLIGHT,
1508 ctrl_param, NULL);
AceLan Kao6e0044b2012-03-20 09:53:09 +01001509 if (asus->driver->quirks->store_backlight_power)
1510 asus->driver->panel_power = bd->props.power;
AceLan Kao6e0044b2012-03-20 09:53:09 +01001511
Corentin Charyade28ab2012-03-20 09:53:14 +01001512 /* When using scalar brightness, updating the brightness
1513 * will mess with the backlight power */
1514 if (asus->driver->quirks->scalar_panel_brightness)
1515 return err;
Corentin Charyb7187262011-02-06 13:28:39 +01001516 }
Corentin Charyade28ab2012-03-20 09:53:14 +01001517
1518 if (asus->driver->quirks->scalar_panel_brightness)
1519 ctrl_param = get_scalar_command(bd);
1520 else
1521 ctrl_param = bd->props.brightness;
1522
1523 err = asus_wmi_set_devstate(ASUS_WMI_DEVID_BRIGHTNESS,
1524 ctrl_param, NULL);
1525
Corentin Chary8fbea012011-02-26 10:20:37 +01001526 return err;
Yong Wang3d7b1652010-04-11 09:27:54 +08001527}
1528
Corentin Charye12e6d92011-02-26 10:20:31 +01001529static const struct backlight_ops asus_wmi_bl_ops = {
Yong Wang3d7b1652010-04-11 09:27:54 +08001530 .get_brightness = read_brightness,
1531 .update_status = update_bl_status,
1532};
1533
Corentin Charye12e6d92011-02-26 10:20:31 +01001534static int asus_wmi_backlight_notify(struct asus_wmi *asus, int code)
Yong Wang3d7b1652010-04-11 09:27:54 +08001535{
Corentin Charye12e6d92011-02-26 10:20:31 +01001536 struct backlight_device *bd = asus->backlight_device;
Yong Wang3d7b1652010-04-11 09:27:54 +08001537 int old = bd->props.brightness;
Daniel Mackb7670ed2010-05-19 12:37:01 +02001538 int new = old;
Yong Wang3d7b1652010-04-11 09:27:54 +08001539
1540 if (code >= NOTIFY_BRNUP_MIN && code <= NOTIFY_BRNUP_MAX)
1541 new = code - NOTIFY_BRNUP_MIN + 1;
1542 else if (code >= NOTIFY_BRNDOWN_MIN && code <= NOTIFY_BRNDOWN_MAX)
1543 new = code - NOTIFY_BRNDOWN_MIN;
1544
1545 bd->props.brightness = new;
1546 backlight_update_status(bd);
1547 backlight_force_update(bd, BACKLIGHT_UPDATE_HOTKEY);
1548
1549 return old;
1550}
1551
Corentin Charye12e6d92011-02-26 10:20:31 +01001552static int asus_wmi_backlight_init(struct asus_wmi *asus)
Yong Wang3d7b1652010-04-11 09:27:54 +08001553{
1554 struct backlight_device *bd;
1555 struct backlight_properties props;
Corentin Charyb7187262011-02-06 13:28:39 +01001556 int max;
1557 int power;
1558
Corentin Chary8fbea012011-02-26 10:20:37 +01001559 max = read_brightness_max(asus);
Hans de Goede86ac27352014-07-08 10:47:22 +02001560 if (max < 0)
Corentin Chary8fbea012011-02-26 10:20:37 +01001561 return max;
1562
1563 power = read_backlight_power(asus);
1564
Corentin Charyb7187262011-02-06 13:28:39 +01001565 if (power == -ENODEV)
1566 power = FB_BLANK_UNBLANK;
Corentin Chary8fbea012011-02-26 10:20:37 +01001567 else if (power < 0)
1568 return power;
Yong Wang3d7b1652010-04-11 09:27:54 +08001569
1570 memset(&props, 0, sizeof(struct backlight_properties));
Axel Lin60cfa092011-06-29 11:43:30 +08001571 props.type = BACKLIGHT_PLATFORM;
Corentin Charyb7187262011-02-06 13:28:39 +01001572 props.max_brightness = max;
Corentin Charye12e6d92011-02-26 10:20:31 +01001573 bd = backlight_device_register(asus->driver->name,
1574 &asus->platform_device->dev, asus,
1575 &asus_wmi_bl_ops, &props);
Yong Wang3d7b1652010-04-11 09:27:54 +08001576 if (IS_ERR(bd)) {
1577 pr_err("Could not register backlight device\n");
1578 return PTR_ERR(bd);
1579 }
1580
Corentin Charye12e6d92011-02-26 10:20:31 +01001581 asus->backlight_device = bd;
Yong Wang3d7b1652010-04-11 09:27:54 +08001582
AceLan Kao6e0044b2012-03-20 09:53:09 +01001583 if (asus->driver->quirks->store_backlight_power)
1584 asus->driver->panel_power = power;
1585
Yong Wang3d7b1652010-04-11 09:27:54 +08001586 bd->props.brightness = read_brightness(bd);
Corentin Charyb7187262011-02-06 13:28:39 +01001587 bd->props.power = power;
Yong Wang3d7b1652010-04-11 09:27:54 +08001588 backlight_update_status(bd);
1589
AceLan Kaoc87992d2012-03-20 09:53:08 +01001590 asus->driver->brightness = bd->props.brightness;
1591
Yong Wang3d7b1652010-04-11 09:27:54 +08001592 return 0;
1593}
1594
Corentin Charye12e6d92011-02-26 10:20:31 +01001595static void asus_wmi_backlight_exit(struct asus_wmi *asus)
Yong Wang3d7b1652010-04-11 09:27:54 +08001596{
Markus Elfring00981812014-11-24 20:30:29 +01001597 backlight_device_unregister(asus->backlight_device);
Yong Wang3d7b1652010-04-11 09:27:54 +08001598
Corentin Charye12e6d92011-02-26 10:20:31 +01001599 asus->backlight_device = NULL;
Yong Wang3d7b1652010-04-11 09:27:54 +08001600}
1601
AceLan Kaoa2a96f02012-10-03 11:26:31 +02001602static int is_display_toggle(int code)
1603{
1604 /* display toggle keys */
1605 if ((code >= 0x61 && code <= 0x67) ||
1606 (code >= 0x8c && code <= 0x93) ||
1607 (code >= 0xa0 && code <= 0xa7) ||
1608 (code >= 0xd0 && code <= 0xd5))
1609 return 1;
1610
1611 return 0;
1612}
1613
Chris Chiu487579b2019-04-18 14:46:48 +08001614static bool asus_wmi_has_fnlock_key(struct asus_wmi *asus)
1615{
1616 u32 result;
1617
1618 asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_FNLOCK, &result);
1619
1620 return (result & ASUS_WMI_DSTS_PRESENCE_BIT) &&
1621 !(result & ASUS_WMI_FNLOCK_BIOS_DISABLED);
1622}
1623
1624static void asus_wmi_fnlock_update(struct asus_wmi *asus)
1625{
1626 int mode = asus->fnlock_locked;
1627
1628 asus_wmi_set_devstate(ASUS_WMI_DEVID_FNLOCK, mode, NULL);
1629}
1630
Corentin Charye12e6d92011-02-26 10:20:31 +01001631static void asus_wmi_notify(u32 value, void *context)
Yong Wang3d7b1652010-04-11 09:27:54 +08001632{
Corentin Charye12e6d92011-02-26 10:20:31 +01001633 struct asus_wmi *asus = context;
Yong Wang3d7b1652010-04-11 09:27:54 +08001634 struct acpi_buffer response = { ACPI_ALLOCATE_BUFFER, NULL };
1635 union acpi_object *obj;
1636 acpi_status status;
1637 int code;
1638 int orig_code;
Seth Forsheec4453f62011-07-01 11:34:27 +02001639 unsigned int key_value = 1;
1640 bool autorelease = 1;
Yong Wang3d7b1652010-04-11 09:27:54 +08001641
1642 status = wmi_get_event_data(value, &response);
1643 if (status != AE_OK) {
1644 pr_err("bad event status 0x%x\n", status);
1645 return;
1646 }
1647
1648 obj = (union acpi_object *)response.pointer;
1649
Corentin Chary57ab7da2011-02-26 10:20:32 +01001650 if (!obj || obj->type != ACPI_TYPE_INTEGER)
1651 goto exit;
Yong Wang3d7b1652010-04-11 09:27:54 +08001652
Corentin Chary57ab7da2011-02-26 10:20:32 +01001653 code = obj->integer.value;
1654 orig_code = code;
Yong Wang3d7b1652010-04-11 09:27:54 +08001655
Seth Forsheec4453f62011-07-01 11:34:27 +02001656 if (asus->driver->key_filter) {
1657 asus->driver->key_filter(asus->driver, &code, &key_value,
1658 &autorelease);
1659 if (code == ASUS_WMI_KEY_IGNORE)
1660 goto exit;
1661 }
1662
Corentin Chary57ab7da2011-02-26 10:20:32 +01001663 if (code >= NOTIFY_BRNUP_MIN && code <= NOTIFY_BRNUP_MAX)
Corentin Chary3ba03022012-11-29 09:12:38 +01001664 code = ASUS_WMI_BRN_UP;
Corentin Chary57ab7da2011-02-26 10:20:32 +01001665 else if (code >= NOTIFY_BRNDOWN_MIN &&
1666 code <= NOTIFY_BRNDOWN_MAX)
Corentin Chary3ba03022012-11-29 09:12:38 +01001667 code = ASUS_WMI_BRN_DOWN;
Yong Wang3d7b1652010-04-11 09:27:54 +08001668
Corentin Chary3ba03022012-11-29 09:12:38 +01001669 if (code == ASUS_WMI_BRN_DOWN || code == ASUS_WMI_BRN_UP) {
Hans de Goede62c4aa12015-06-16 16:27:58 +02001670 if (acpi_video_get_backlight_type() == acpi_backlight_vendor) {
Corentin Chary57ab7da2011-02-26 10:20:32 +01001671 asus_wmi_backlight_notify(asus, orig_code);
Corentin Chary3ba03022012-11-29 09:12:38 +01001672 goto exit;
AceLan Kaoa2a96f02012-10-03 11:26:31 +02001673 }
AceLan Kaoa2a96f02012-10-03 11:26:31 +02001674 }
1675
Chris Chiudbb3d782018-06-20 22:46:44 +08001676 if (code == NOTIFY_KBD_BRTUP) {
Jian-Hong Pan29f6eb52018-10-22 18:00:04 +08001677 kbd_led_set_by_kbd(asus, asus->kbd_led_wk + 1);
Chris Chiudbb3d782018-06-20 22:46:44 +08001678 goto exit;
1679 }
1680 if (code == NOTIFY_KBD_BRTDWN) {
Jian-Hong Pan29f6eb52018-10-22 18:00:04 +08001681 kbd_led_set_by_kbd(asus, asus->kbd_led_wk - 1);
Chris Chiudbb3d782018-06-20 22:46:44 +08001682 goto exit;
1683 }
Chris Chiued99d292018-06-20 22:46:45 +08001684 if (code == NOTIFY_KBD_BRTTOGGLE) {
1685 if (asus->kbd_led_wk == asus->kbd_led.max_brightness)
Jian-Hong Pan29f6eb52018-10-22 18:00:04 +08001686 kbd_led_set_by_kbd(asus, 0);
Chris Chiued99d292018-06-20 22:46:45 +08001687 else
Jian-Hong Pan29f6eb52018-10-22 18:00:04 +08001688 kbd_led_set_by_kbd(asus, asus->kbd_led_wk + 1);
Chris Chiued99d292018-06-20 22:46:45 +08001689 goto exit;
1690 }
Chris Chiudbb3d782018-06-20 22:46:44 +08001691
Chris Chiu487579b2019-04-18 14:46:48 +08001692 if (code == NOTIFY_FNLOCK_TOGGLE) {
1693 asus->fnlock_locked = !asus->fnlock_locked;
1694 asus_wmi_fnlock_update(asus);
1695 goto exit;
1696 }
1697
AceLan Kaoa2a96f02012-10-03 11:26:31 +02001698 if (is_display_toggle(code) &&
1699 asus->driver->quirks->no_display_toggle)
1700 goto exit;
1701
1702 if (!sparse_keymap_report_event(asus->inputdev, code,
1703 key_value, autorelease))
Corentin Chary57ab7da2011-02-26 10:20:32 +01001704 pr_info("Unknown key %x pressed\n", code);
Yong Wang3d7b1652010-04-11 09:27:54 +08001705
Corentin Chary57ab7da2011-02-26 10:20:32 +01001706exit:
Yong Wang3d7b1652010-04-11 09:27:54 +08001707 kfree(obj);
1708}
1709
Corentin Chary9e1565b2011-02-06 13:28:36 +01001710/*
1711 * Sys helpers
1712 */
1713static int parse_arg(const char *buf, unsigned long count, int *val)
1714{
1715 if (!count)
1716 return 0;
1717 if (sscanf(buf, "%i", val) != 1)
1718 return -EINVAL;
1719 return count;
1720}
1721
Corentin Chary1d070f82011-02-26 10:20:36 +01001722static ssize_t store_sys_wmi(struct asus_wmi *asus, int devid,
1723 const char *buf, size_t count)
Corentin Chary9e1565b2011-02-06 13:28:36 +01001724{
Corentin Chary9e1565b2011-02-06 13:28:36 +01001725 u32 retval;
Corentin Charyd33da3b2011-02-26 10:20:35 +01001726 int rv, err, value;
Corentin Chary9e1565b2011-02-06 13:28:36 +01001727
Corentin Chary1d070f82011-02-26 10:20:36 +01001728 value = asus_wmi_get_devstate_simple(asus, devid);
Dan Carpenterb8298342015-11-11 01:18:16 +03001729 if (value < 0)
Corentin Chary9e1565b2011-02-06 13:28:36 +01001730 return value;
1731
1732 rv = parse_arg(buf, count, &value);
Corentin Charyd33da3b2011-02-26 10:20:35 +01001733 err = asus_wmi_set_devstate(devid, value, &retval);
Corentin Chary9e1565b2011-02-06 13:28:36 +01001734
Corentin Charyd33da3b2011-02-26 10:20:35 +01001735 if (err < 0)
1736 return err;
1737
Corentin Chary9e1565b2011-02-06 13:28:36 +01001738 return rv;
1739}
1740
Corentin Chary1d070f82011-02-26 10:20:36 +01001741static ssize_t show_sys_wmi(struct asus_wmi *asus, int devid, char *buf)
Corentin Chary9e1565b2011-02-06 13:28:36 +01001742{
Corentin Chary1d070f82011-02-26 10:20:36 +01001743 int value = asus_wmi_get_devstate_simple(asus, devid);
Corentin Chary9e1565b2011-02-06 13:28:36 +01001744
1745 if (value < 0)
1746 return value;
1747
1748 return sprintf(buf, "%d\n", value);
1749}
1750
Corentin Charye12e6d92011-02-26 10:20:31 +01001751#define ASUS_WMI_CREATE_DEVICE_ATTR(_name, _mode, _cm) \
Corentin Chary9e1565b2011-02-06 13:28:36 +01001752 static ssize_t show_##_name(struct device *dev, \
1753 struct device_attribute *attr, \
1754 char *buf) \
1755 { \
Corentin Chary1d070f82011-02-26 10:20:36 +01001756 struct asus_wmi *asus = dev_get_drvdata(dev); \
1757 \
1758 return show_sys_wmi(asus, _cm, buf); \
Corentin Chary9e1565b2011-02-06 13:28:36 +01001759 } \
1760 static ssize_t store_##_name(struct device *dev, \
1761 struct device_attribute *attr, \
1762 const char *buf, size_t count) \
1763 { \
Corentin Chary1d070f82011-02-26 10:20:36 +01001764 struct asus_wmi *asus = dev_get_drvdata(dev); \
1765 \
1766 return store_sys_wmi(asus, _cm, buf, count); \
Corentin Chary9e1565b2011-02-06 13:28:36 +01001767 } \
1768 static struct device_attribute dev_attr_##_name = { \
1769 .attr = { \
1770 .name = __stringify(_name), \
1771 .mode = _mode }, \
1772 .show = show_##_name, \
1773 .store = store_##_name, \
1774 }
1775
Corentin Charye12e6d92011-02-26 10:20:31 +01001776ASUS_WMI_CREATE_DEVICE_ATTR(touchpad, 0644, ASUS_WMI_DEVID_TOUCHPAD);
1777ASUS_WMI_CREATE_DEVICE_ATTR(camera, 0644, ASUS_WMI_DEVID_CAMERA);
1778ASUS_WMI_CREATE_DEVICE_ATTR(cardr, 0644, ASUS_WMI_DEVID_CARDREADER);
AceLan Kaoc0b91b62012-06-13 09:32:07 +02001779ASUS_WMI_CREATE_DEVICE_ATTR(lid_resume, 0644, ASUS_WMI_DEVID_LID_RESUME);
Oleksij Rempelaca234f2016-04-01 13:35:21 +02001780ASUS_WMI_CREATE_DEVICE_ATTR(als_enable, 0644, ASUS_WMI_DEVID_ALS_ENABLE);
Corentin Chary9e1565b2011-02-06 13:28:36 +01001781
Jérémy Lefaurea8fe3422017-04-21 22:19:45 -04001782static ssize_t cpufv_store(struct device *dev, struct device_attribute *attr,
Dmitry Torokhov67fa38e2010-11-03 11:14:01 -07001783 const char *buf, size_t count)
Chris Bagwell7f80d732010-10-11 18:47:18 -05001784{
Corentin Chary3df5fda2011-07-01 11:34:38 +02001785 int value, rv;
Chris Bagwell7f80d732010-10-11 18:47:18 -05001786
1787 if (!count || sscanf(buf, "%i", &value) != 1)
1788 return -EINVAL;
1789 if (value < 0 || value > 2)
1790 return -EINVAL;
1791
Corentin Chary3df5fda2011-07-01 11:34:38 +02001792 rv = asus_wmi_evaluate_method(ASUS_WMI_METHODID_CFVS, value, 0, NULL);
1793 if (rv < 0)
1794 return rv;
1795
1796 return count;
Chris Bagwell7f80d732010-10-11 18:47:18 -05001797}
1798
Jérémy Lefaurea8fe3422017-04-21 22:19:45 -04001799static DEVICE_ATTR_WO(cpufv);
Chris Bagwell7f80d732010-10-11 18:47:18 -05001800
Corentin Chary4e37b422010-11-29 08:14:08 +01001801static struct attribute *platform_attributes[] = {
1802 &dev_attr_cpufv.attr,
Corentin Chary9e1565b2011-02-06 13:28:36 +01001803 &dev_attr_camera.attr,
1804 &dev_attr_cardr.attr,
Corentin Chary4615bb62011-02-06 13:28:42 +01001805 &dev_attr_touchpad.attr,
AceLan Kaoc0b91b62012-06-13 09:32:07 +02001806 &dev_attr_lid_resume.attr,
Oleksij Rempelaca234f2016-04-01 13:35:21 +02001807 &dev_attr_als_enable.attr,
Corentin Chary4e37b422010-11-29 08:14:08 +01001808 NULL
1809};
1810
Al Viro587a1f12011-07-23 23:11:19 -04001811static umode_t asus_sysfs_is_visible(struct kobject *kobj,
Corentin Charye12e6d92011-02-26 10:20:31 +01001812 struct attribute *attr, int idx)
Corentin Chary9e1565b2011-02-06 13:28:36 +01001813{
Corentin Chary1d070f82011-02-26 10:20:36 +01001814 struct device *dev = container_of(kobj, struct device, kobj);
Wolfram Sangd605ca22018-04-19 16:06:10 +02001815 struct asus_wmi *asus = dev_get_drvdata(dev);
Corentin Chary1d070f82011-02-26 10:20:36 +01001816 bool ok = true;
Corentin Chary9e1565b2011-02-06 13:28:36 +01001817 int devid = -1;
1818
1819 if (attr == &dev_attr_camera.attr)
Corentin Charye12e6d92011-02-26 10:20:31 +01001820 devid = ASUS_WMI_DEVID_CAMERA;
Corentin Chary9e1565b2011-02-06 13:28:36 +01001821 else if (attr == &dev_attr_cardr.attr)
Corentin Charye12e6d92011-02-26 10:20:31 +01001822 devid = ASUS_WMI_DEVID_CARDREADER;
Corentin Chary4615bb62011-02-06 13:28:42 +01001823 else if (attr == &dev_attr_touchpad.attr)
Corentin Charye12e6d92011-02-26 10:20:31 +01001824 devid = ASUS_WMI_DEVID_TOUCHPAD;
AceLan Kaoc0b91b62012-06-13 09:32:07 +02001825 else if (attr == &dev_attr_lid_resume.attr)
1826 devid = ASUS_WMI_DEVID_LID_RESUME;
Oleksij Rempelaca234f2016-04-01 13:35:21 +02001827 else if (attr == &dev_attr_als_enable.attr)
1828 devid = ASUS_WMI_DEVID_ALS_ENABLE;
Corentin Chary9e1565b2011-02-06 13:28:36 +01001829
1830 if (devid != -1)
Corentin Chary1d070f82011-02-26 10:20:36 +01001831 ok = !(asus_wmi_get_devstate_simple(asus, devid) < 0);
Corentin Chary9e1565b2011-02-06 13:28:36 +01001832
Corentin Chary1d070f82011-02-26 10:20:36 +01001833 return ok ? attr->mode : 0;
Corentin Chary9e1565b2011-02-06 13:28:36 +01001834}
1835
Arvind Yadave90d9ba2017-07-11 16:18:19 +05301836static const struct attribute_group platform_attribute_group = {
Corentin Charye12e6d92011-02-26 10:20:31 +01001837 .is_visible = asus_sysfs_is_visible,
1838 .attrs = platform_attributes
Corentin Chary4e37b422010-11-29 08:14:08 +01001839};
1840
Corentin Charye12e6d92011-02-26 10:20:31 +01001841static void asus_wmi_sysfs_exit(struct platform_device *device)
Chris Bagwell7f80d732010-10-11 18:47:18 -05001842{
Corentin Chary4e37b422010-11-29 08:14:08 +01001843 sysfs_remove_group(&device->dev.kobj, &platform_attribute_group);
Chris Bagwell7f80d732010-10-11 18:47:18 -05001844}
1845
Corentin Charye12e6d92011-02-26 10:20:31 +01001846static int asus_wmi_sysfs_init(struct platform_device *device)
Chris Bagwell7f80d732010-10-11 18:47:18 -05001847{
Corentin Chary4e37b422010-11-29 08:14:08 +01001848 return sysfs_create_group(&device->dev.kobj, &platform_attribute_group);
Chris Bagwell7f80d732010-10-11 18:47:18 -05001849}
1850
Corentin Chary27c136c2010-11-29 08:14:05 +01001851/*
1852 * Platform device
1853 */
Joe Perches39ddf3b2011-03-29 15:21:32 -07001854static int asus_wmi_platform_init(struct asus_wmi *asus)
Corentin Chary27c136c2010-11-29 08:14:05 +01001855{
Corentin Chary46dbca82011-02-26 10:20:38 +01001856 int rv;
1857
1858 /* INIT enable hotkeys on some models */
1859 if (!asus_wmi_evaluate_method(ASUS_WMI_METHODID_INIT, 0, 0, &rv))
vic0ed60652013-05-22 21:32:10 +03001860 pr_info("Initialization: %#x\n", rv);
Corentin Chary46dbca82011-02-26 10:20:38 +01001861
1862 /* We don't know yet what to do with this version... */
1863 if (!asus_wmi_evaluate_method(ASUS_WMI_METHODID_SPEC, 0, 0x9, &rv)) {
vic0ed60652013-05-22 21:32:10 +03001864 pr_info("BIOS WMI version: %d.%d\n", rv >> 16, rv & 0xFF);
Corentin Chary46dbca82011-02-26 10:20:38 +01001865 asus->spec = rv;
1866 }
1867
1868 /*
1869 * The SFUN method probably allows the original driver to get the list
1870 * of features supported by a given model. For now, 0x0100 or 0x0800
1871 * bit signifies that the laptop is equipped with a Wi-Fi MiniPCI card.
1872 * The significance of others is yet to be found.
1873 */
1874 if (!asus_wmi_evaluate_method(ASUS_WMI_METHODID_SFUN, 0, 0, &rv)) {
vic0ed60652013-05-22 21:32:10 +03001875 pr_info("SFUN value: %#x\n", rv);
Corentin Chary46dbca82011-02-26 10:20:38 +01001876 asus->sfun = rv;
1877 }
1878
Corentin Chary1d070f82011-02-26 10:20:36 +01001879 /*
1880 * Eee PC and Notebooks seems to have different method_id for DSTS,
1881 * but it may also be related to the BIOS's SPEC.
1882 * Note, on most Eeepc, there is no way to check if a method exist
1883 * or note, while on notebooks, they returns 0xFFFFFFFE on failure,
1884 * but once again, SPEC may probably be used for that kind of things.
1885 */
1886 if (!asus_wmi_evaluate_method(ASUS_WMI_METHODID_DSTS, 0, 0, NULL))
1887 asus->dsts_id = ASUS_WMI_METHODID_DSTS;
Alex Hung63a78bb12012-06-20 11:47:35 +08001888 else
Corentin Chary1d070f82011-02-26 10:20:36 +01001889 asus->dsts_id = ASUS_WMI_METHODID_DSTS2;
1890
Corentin Charyfddbfed2011-07-01 11:34:39 +02001891 /* CWAP allow to define the behavior of the Fn+F2 key,
1892 * this method doesn't seems to be present on Eee PCs */
Corentin Chary6a2bccc2012-03-20 09:53:10 +01001893 if (asus->driver->quirks->wapf >= 0)
Corentin Charyfddbfed2011-07-01 11:34:39 +02001894 asus_wmi_set_devstate(ASUS_WMI_DEVID_CWAP,
Corentin Chary6a2bccc2012-03-20 09:53:10 +01001895 asus->driver->quirks->wapf, NULL);
Corentin Charyfddbfed2011-07-01 11:34:39 +02001896
Corentin Charye12e6d92011-02-26 10:20:31 +01001897 return asus_wmi_sysfs_init(asus->platform_device);
Corentin Chary27c136c2010-11-29 08:14:05 +01001898}
1899
Corentin Charye12e6d92011-02-26 10:20:31 +01001900static void asus_wmi_platform_exit(struct asus_wmi *asus)
Corentin Chary27c136c2010-11-29 08:14:05 +01001901{
Corentin Charye12e6d92011-02-26 10:20:31 +01001902 asus_wmi_sysfs_exit(asus->platform_device);
Corentin Chary27c136c2010-11-29 08:14:05 +01001903}
1904
1905/*
Corentin Chary8c1b2d82010-11-29 08:14:09 +01001906 * debugfs
1907 */
Corentin Charye12e6d92011-02-26 10:20:31 +01001908struct asus_wmi_debugfs_node {
1909 struct asus_wmi *asus;
Corentin Chary8c1b2d82010-11-29 08:14:09 +01001910 char *name;
Corentin Charye12e6d92011-02-26 10:20:31 +01001911 int (*show) (struct seq_file *m, void *data);
Corentin Chary8c1b2d82010-11-29 08:14:09 +01001912};
1913
1914static int show_dsts(struct seq_file *m, void *data)
1915{
Corentin Charye12e6d92011-02-26 10:20:31 +01001916 struct asus_wmi *asus = m->private;
Corentin Charyd33da3b2011-02-26 10:20:35 +01001917 int err;
Corentin Chary8c1b2d82010-11-29 08:14:09 +01001918 u32 retval = -1;
1919
Corentin Chary1d070f82011-02-26 10:20:36 +01001920 err = asus_wmi_get_devstate(asus, asus->debug.dev_id, &retval);
Corentin Chary8c1b2d82010-11-29 08:14:09 +01001921
Corentin Charyd33da3b2011-02-26 10:20:35 +01001922 if (err < 0)
1923 return err;
Corentin Chary8c1b2d82010-11-29 08:14:09 +01001924
Corentin Charyef343492011-02-26 10:20:39 +01001925 seq_printf(m, "DSTS(%#x) = %#x\n", asus->debug.dev_id, retval);
Corentin Chary8c1b2d82010-11-29 08:14:09 +01001926
1927 return 0;
1928}
1929
1930static int show_devs(struct seq_file *m, void *data)
1931{
Corentin Charye12e6d92011-02-26 10:20:31 +01001932 struct asus_wmi *asus = m->private;
Corentin Charyd33da3b2011-02-26 10:20:35 +01001933 int err;
Corentin Chary8c1b2d82010-11-29 08:14:09 +01001934 u32 retval = -1;
1935
Corentin Charyd33da3b2011-02-26 10:20:35 +01001936 err = asus_wmi_set_devstate(asus->debug.dev_id, asus->debug.ctrl_param,
1937 &retval);
1938
1939 if (err < 0)
1940 return err;
Corentin Chary8c1b2d82010-11-29 08:14:09 +01001941
Corentin Charyef343492011-02-26 10:20:39 +01001942 seq_printf(m, "DEVS(%#x, %#x) = %#x\n", asus->debug.dev_id,
Corentin Charye12e6d92011-02-26 10:20:31 +01001943 asus->debug.ctrl_param, retval);
Corentin Chary8c1b2d82010-11-29 08:14:09 +01001944
1945 return 0;
1946}
1947
Corentin Charyef343492011-02-26 10:20:39 +01001948static int show_call(struct seq_file *m, void *data)
1949{
1950 struct asus_wmi *asus = m->private;
1951 struct bios_args args = {
1952 .arg0 = asus->debug.dev_id,
1953 .arg1 = asus->debug.ctrl_param,
1954 };
1955 struct acpi_buffer input = { (acpi_size) sizeof(args), &args };
1956 struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER, NULL };
1957 union acpi_object *obj;
1958 acpi_status status;
1959
1960 status = wmi_evaluate_method(ASUS_WMI_MGMT_GUID,
Pali Rohár0fe57262017-08-12 09:44:16 +02001961 0, asus->debug.method_id,
Corentin Charyef343492011-02-26 10:20:39 +01001962 &input, &output);
1963
1964 if (ACPI_FAILURE(status))
1965 return -EIO;
1966
1967 obj = (union acpi_object *)output.pointer;
1968 if (obj && obj->type == ACPI_TYPE_INTEGER)
1969 seq_printf(m, "%#x(%#x, %#x) = %#x\n", asus->debug.method_id,
1970 asus->debug.dev_id, asus->debug.ctrl_param,
1971 (u32) obj->integer.value);
1972 else
1973 seq_printf(m, "%#x(%#x, %#x) = t:%d\n", asus->debug.method_id,
1974 asus->debug.dev_id, asus->debug.ctrl_param,
Dan Carpentera1d60862011-03-15 10:07:37 +03001975 obj ? obj->type : -1);
Corentin Charyef343492011-02-26 10:20:39 +01001976
1977 kfree(obj);
1978
1979 return 0;
1980}
1981
Corentin Charye12e6d92011-02-26 10:20:31 +01001982static struct asus_wmi_debugfs_node asus_wmi_debug_files[] = {
1983 {NULL, "devs", show_devs},
1984 {NULL, "dsts", show_dsts},
Corentin Charyef343492011-02-26 10:20:39 +01001985 {NULL, "call", show_call},
Corentin Chary8c1b2d82010-11-29 08:14:09 +01001986};
1987
Corentin Charye12e6d92011-02-26 10:20:31 +01001988static int asus_wmi_debugfs_open(struct inode *inode, struct file *file)
Corentin Chary8c1b2d82010-11-29 08:14:09 +01001989{
Corentin Charye12e6d92011-02-26 10:20:31 +01001990 struct asus_wmi_debugfs_node *node = inode->i_private;
Corentin Chary8c1b2d82010-11-29 08:14:09 +01001991
Corentin Charye12e6d92011-02-26 10:20:31 +01001992 return single_open(file, node->show, node->asus);
Corentin Chary8c1b2d82010-11-29 08:14:09 +01001993}
1994
Corentin Charye12e6d92011-02-26 10:20:31 +01001995static const struct file_operations asus_wmi_debugfs_io_ops = {
Corentin Chary8c1b2d82010-11-29 08:14:09 +01001996 .owner = THIS_MODULE,
Corentin Charye12e6d92011-02-26 10:20:31 +01001997 .open = asus_wmi_debugfs_open,
Corentin Chary8c1b2d82010-11-29 08:14:09 +01001998 .read = seq_read,
1999 .llseek = seq_lseek,
2000 .release = single_release,
2001};
2002
Corentin Charye12e6d92011-02-26 10:20:31 +01002003static void asus_wmi_debugfs_exit(struct asus_wmi *asus)
Corentin Chary8c1b2d82010-11-29 08:14:09 +01002004{
Corentin Charye12e6d92011-02-26 10:20:31 +01002005 debugfs_remove_recursive(asus->debug.root);
Corentin Chary8c1b2d82010-11-29 08:14:09 +01002006}
2007
Corentin Charye12e6d92011-02-26 10:20:31 +01002008static int asus_wmi_debugfs_init(struct asus_wmi *asus)
Corentin Chary8c1b2d82010-11-29 08:14:09 +01002009{
2010 struct dentry *dent;
2011 int i;
2012
Corentin Charye12e6d92011-02-26 10:20:31 +01002013 asus->debug.root = debugfs_create_dir(asus->driver->name, NULL);
2014 if (!asus->debug.root) {
vic0ed60652013-05-22 21:32:10 +03002015 pr_err("failed to create debugfs directory\n");
Corentin Chary8c1b2d82010-11-29 08:14:09 +01002016 goto error_debugfs;
2017 }
2018
Corentin Charyef343492011-02-26 10:20:39 +01002019 dent = debugfs_create_x32("method_id", S_IRUGO | S_IWUSR,
2020 asus->debug.root, &asus->debug.method_id);
2021 if (!dent)
2022 goto error_debugfs;
2023
Corentin Charye12e6d92011-02-26 10:20:31 +01002024 dent = debugfs_create_x32("dev_id", S_IRUGO | S_IWUSR,
2025 asus->debug.root, &asus->debug.dev_id);
Corentin Chary8c1b2d82010-11-29 08:14:09 +01002026 if (!dent)
2027 goto error_debugfs;
2028
Corentin Charye12e6d92011-02-26 10:20:31 +01002029 dent = debugfs_create_x32("ctrl_param", S_IRUGO | S_IWUSR,
2030 asus->debug.root, &asus->debug.ctrl_param);
Corentin Chary8c1b2d82010-11-29 08:14:09 +01002031 if (!dent)
2032 goto error_debugfs;
2033
Corentin Charye12e6d92011-02-26 10:20:31 +01002034 for (i = 0; i < ARRAY_SIZE(asus_wmi_debug_files); i++) {
2035 struct asus_wmi_debugfs_node *node = &asus_wmi_debug_files[i];
Corentin Chary8c1b2d82010-11-29 08:14:09 +01002036
Corentin Charye12e6d92011-02-26 10:20:31 +01002037 node->asus = asus;
Corentin Chary8c1b2d82010-11-29 08:14:09 +01002038 dent = debugfs_create_file(node->name, S_IFREG | S_IRUGO,
Corentin Charye12e6d92011-02-26 10:20:31 +01002039 asus->debug.root, node,
2040 &asus_wmi_debugfs_io_ops);
Corentin Chary8c1b2d82010-11-29 08:14:09 +01002041 if (!dent) {
2042 pr_err("failed to create debug file: %s\n", node->name);
2043 goto error_debugfs;
2044 }
2045 }
2046
2047 return 0;
2048
2049error_debugfs:
Corentin Charye12e6d92011-02-26 10:20:31 +01002050 asus_wmi_debugfs_exit(asus);
Corentin Chary8c1b2d82010-11-29 08:14:09 +01002051 return -ENOMEM;
2052}
2053
Kast Bernd53e755c2015-05-13 16:24:16 +02002054static int asus_wmi_fan_init(struct asus_wmi *asus)
2055{
2056 int status;
2057
2058 asus->asus_hwmon_pwm = -1;
2059 asus->asus_hwmon_num_fans = -1;
2060 asus->asus_hwmon_fan_manual_mode = false;
2061
2062 status = asus_hwmon_get_fan_number(asus, &asus->asus_hwmon_num_fans);
2063 if (status) {
2064 asus->asus_hwmon_num_fans = 0;
2065 pr_warn("Could not determine number of fans: %d\n", status);
2066 return -ENXIO;
2067 }
2068
2069 pr_info("Number of fans: %d\n", asus->asus_hwmon_num_fans);
2070 return 0;
2071}
2072
Corentin Chary8c1b2d82010-11-29 08:14:09 +01002073/*
Corentin Chary27c136c2010-11-29 08:14:05 +01002074 * WMI Driver
2075 */
Corentin Charye12e6d92011-02-26 10:20:31 +01002076static int asus_wmi_add(struct platform_device *pdev)
Corentin Charyafa7c882011-02-06 13:28:28 +01002077{
Corentin Charye12e6d92011-02-26 10:20:31 +01002078 struct platform_driver *pdrv = to_platform_driver(pdev->dev.driver);
2079 struct asus_wmi_driver *wdrv = to_asus_wmi_driver(pdrv);
2080 struct asus_wmi *asus;
Hans de Goeded8c66f62014-07-08 10:47:21 +02002081 const char *chassis_type;
Yong Wangee027e42010-03-21 10:26:34 +08002082 acpi_status status;
Corentin Chary27c136c2010-11-29 08:14:05 +01002083 int err;
AceLan Kaoa50bd122012-07-26 17:13:31 +08002084 u32 result;
Yong Wangee027e42010-03-21 10:26:34 +08002085
Corentin Charye12e6d92011-02-26 10:20:31 +01002086 asus = kzalloc(sizeof(struct asus_wmi), GFP_KERNEL);
2087 if (!asus)
Corentin Charya04ce292011-02-06 13:28:33 +01002088 return -ENOMEM;
2089
Corentin Charye12e6d92011-02-26 10:20:31 +01002090 asus->driver = wdrv;
2091 asus->platform_device = pdev;
2092 wdrv->platform_device = pdev;
2093 platform_set_drvdata(asus->platform_device, asus);
Corentin Chary27c136c2010-11-29 08:14:05 +01002094
AceLan Kaoc87992d2012-03-20 09:53:08 +01002095 if (wdrv->detect_quirks)
2096 wdrv->detect_quirks(asus->driver);
Corentin Charyafa7c882011-02-06 13:28:28 +01002097
Corentin Charye12e6d92011-02-26 10:20:31 +01002098 err = asus_wmi_platform_init(asus);
Corentin Chary27c136c2010-11-29 08:14:05 +01002099 if (err)
2100 goto fail_platform;
Yong Wang45f2c692010-04-11 09:27:19 +08002101
Corentin Charye12e6d92011-02-26 10:20:31 +01002102 err = asus_wmi_input_init(asus);
Yong Wang45f2c692010-04-11 09:27:19 +08002103 if (err)
Corentin Chary27c136c2010-11-29 08:14:05 +01002104 goto fail_input;
Yong Wang3d7b1652010-04-11 09:27:54 +08002105
Kast Bernd53e755c2015-05-13 16:24:16 +02002106 err = asus_wmi_fan_init(asus); /* probably no problems on error */
2107 asus_hwmon_fan_set_auto(asus);
2108
Corentin Charye07babd2011-02-26 10:20:42 +01002109 err = asus_wmi_hwmon_init(asus);
2110 if (err)
2111 goto fail_hwmon;
2112
Corentin Charye12e6d92011-02-26 10:20:31 +01002113 err = asus_wmi_led_init(asus);
Corentin Chary084fca62010-11-29 08:14:06 +01002114 if (err)
2115 goto fail_leds;
2116
João Paulo Rechi Vita71050ae2017-02-20 14:50:22 -05002117 asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_WLAN, &result);
2118 if (result & (ASUS_WMI_DSTS_PRESENCE_BIT | ASUS_WMI_DSTS_USER_BIT))
2119 asus->driver->wlan_ctrl_by_user = 1;
2120
João Paulo Rechi Vitad1c4e9b2017-02-20 14:50:23 -05002121 if (!(asus->driver->wlan_ctrl_by_user && ashs_present())) {
João Paulo Rechi Vitaa977e592016-06-13 16:57:31 -04002122 err = asus_wmi_rfkill_init(asus);
2123 if (err)
2124 goto fail_rfkill;
2125 }
Corentin Charyba48fdb2010-11-29 08:14:07 +01002126
Oleksij Rempele9b61512017-04-28 16:19:49 +02002127 if (asus->driver->quirks->wmi_force_als_set)
2128 asus_wmi_set_als();
2129
Hans de Goeded8c66f62014-07-08 10:47:21 +02002130 /* Some Asus desktop boards export an acpi-video backlight interface,
2131 stop this from showing up */
2132 chassis_type = dmi_get_system_info(DMI_CHASSIS_TYPE);
2133 if (chassis_type && !strcmp(chassis_type, "3"))
Hans de Goede62c4aa12015-06-16 16:27:58 +02002134 acpi_video_set_dmi_backlight_type(acpi_backlight_vendor);
2135
AceLan Kao272c77d2012-06-13 09:32:06 +02002136 if (asus->driver->quirks->wmi_backlight_power)
Hans de Goede62c4aa12015-06-16 16:27:58 +02002137 acpi_video_set_dmi_backlight_type(acpi_backlight_vendor);
2138
zino lin999d4372016-08-28 16:12:06 +08002139 if (asus->driver->quirks->wmi_backlight_native)
2140 acpi_video_set_dmi_backlight_type(acpi_backlight_native);
2141
Kai-Chuan Hsieh8023eff2016-09-01 23:55:55 +08002142 if (asus->driver->quirks->xusb2pr)
2143 asus_wmi_set_xusb2pr(asus);
2144
Hans de Goede62c4aa12015-06-16 16:27:58 +02002145 if (acpi_video_get_backlight_type() == acpi_backlight_vendor) {
Corentin Charye12e6d92011-02-26 10:20:31 +01002146 err = asus_wmi_backlight_init(asus);
Corentin Charyb7187262011-02-06 13:28:39 +01002147 if (err && err != -ENODEV)
Corentin Chary27c136c2010-11-29 08:14:05 +01002148 goto fail_backlight;
Hans de Goede401fee82019-06-12 09:02:02 +02002149 } else if (asus->driver->quirks->wmi_backlight_set_devstate)
João Paulo Rechi Vita78f3ac72018-10-31 17:21:26 -07002150 err = asus_wmi_set_devstate(ASUS_WMI_DEVID_BACKLIGHT, 2, NULL);
Yong Wang45f2c692010-04-11 09:27:19 +08002151
Chris Chiu487579b2019-04-18 14:46:48 +08002152 if (asus_wmi_has_fnlock_key(asus)) {
2153 asus->fnlock_locked = true;
2154 asus_wmi_fnlock_update(asus);
2155 }
2156
Corentin Charye12e6d92011-02-26 10:20:31 +01002157 status = wmi_install_notify_handler(asus->driver->event_guid,
2158 asus_wmi_notify, asus);
Yong Wang45f2c692010-04-11 09:27:19 +08002159 if (ACPI_FAILURE(status)) {
Corentin Charye12e6d92011-02-26 10:20:31 +01002160 pr_err("Unable to register notify handler - %d\n", status);
Yong Wang45f2c692010-04-11 09:27:19 +08002161 err = -ENODEV;
Corentin Chary27c136c2010-11-29 08:14:05 +01002162 goto fail_wmi_handler;
Yong Wang45f2c692010-04-11 09:27:19 +08002163 }
2164
Corentin Charye12e6d92011-02-26 10:20:31 +01002165 err = asus_wmi_debugfs_init(asus);
Corentin Chary8c1b2d82010-11-29 08:14:09 +01002166 if (err)
2167 goto fail_debugfs;
2168
Corentin Charya04ce292011-02-06 13:28:33 +01002169 return 0;
Yong Wang45f2c692010-04-11 09:27:19 +08002170
Corentin Chary8c1b2d82010-11-29 08:14:09 +01002171fail_debugfs:
Corentin Charye12e6d92011-02-26 10:20:31 +01002172 wmi_remove_notify_handler(asus->driver->event_guid);
Corentin Chary27c136c2010-11-29 08:14:05 +01002173fail_wmi_handler:
Corentin Charye12e6d92011-02-26 10:20:31 +01002174 asus_wmi_backlight_exit(asus);
Corentin Chary27c136c2010-11-29 08:14:05 +01002175fail_backlight:
Corentin Charye12e6d92011-02-26 10:20:31 +01002176 asus_wmi_rfkill_exit(asus);
Corentin Charyba48fdb2010-11-29 08:14:07 +01002177fail_rfkill:
Corentin Charye12e6d92011-02-26 10:20:31 +01002178 asus_wmi_led_exit(asus);
Corentin Chary084fca62010-11-29 08:14:06 +01002179fail_leds:
Corentin Charye07babd2011-02-26 10:20:42 +01002180fail_hwmon:
Corentin Charye12e6d92011-02-26 10:20:31 +01002181 asus_wmi_input_exit(asus);
Corentin Chary27c136c2010-11-29 08:14:05 +01002182fail_input:
Corentin Charye12e6d92011-02-26 10:20:31 +01002183 asus_wmi_platform_exit(asus);
Corentin Chary27c136c2010-11-29 08:14:05 +01002184fail_platform:
Corentin Charye12e6d92011-02-26 10:20:31 +01002185 kfree(asus);
Corentin Charya04ce292011-02-06 13:28:33 +01002186 return err;
Yong Wang45f2c692010-04-11 09:27:19 +08002187}
2188
Corentin Charye12e6d92011-02-26 10:20:31 +01002189static int asus_wmi_remove(struct platform_device *device)
Yong Wang45f2c692010-04-11 09:27:19 +08002190{
Corentin Charye12e6d92011-02-26 10:20:31 +01002191 struct asus_wmi *asus;
Yong Wang45f2c692010-04-11 09:27:19 +08002192
Corentin Charye12e6d92011-02-26 10:20:31 +01002193 asus = platform_get_drvdata(device);
2194 wmi_remove_notify_handler(asus->driver->event_guid);
2195 asus_wmi_backlight_exit(asus);
2196 asus_wmi_input_exit(asus);
2197 asus_wmi_led_exit(asus);
2198 asus_wmi_rfkill_exit(asus);
2199 asus_wmi_debugfs_exit(asus);
2200 asus_wmi_platform_exit(asus);
Kast Bernd53e755c2015-05-13 16:24:16 +02002201 asus_hwmon_fan_set_auto(asus);
Yong Wang45f2c692010-04-11 09:27:19 +08002202
Corentin Charye12e6d92011-02-26 10:20:31 +01002203 kfree(asus);
Yong Wang45f2c692010-04-11 09:27:19 +08002204 return 0;
2205}
2206
Corentin Chary0773d7f2011-02-06 13:28:32 +01002207/*
2208 * Platform driver - hibernate/resume callbacks
2209 */
Corentin Charye12e6d92011-02-26 10:20:31 +01002210static int asus_hotk_thaw(struct device *device)
Corentin Chary0773d7f2011-02-06 13:28:32 +01002211{
Corentin Charye12e6d92011-02-26 10:20:31 +01002212 struct asus_wmi *asus = dev_get_drvdata(device);
Corentin Chary0773d7f2011-02-06 13:28:32 +01002213
Corentin Charya7ce3f02011-02-26 10:20:33 +01002214 if (asus->wlan.rfkill) {
Corentin Chary0773d7f2011-02-06 13:28:32 +01002215 bool wlan;
2216
2217 /*
2218 * Work around bios bug - acpi _PTS turns off the wireless led
2219 * during suspend. Normally it restores it on resume, but
2220 * we should kick it ourselves in case hibernation is aborted.
2221 */
Corentin Chary1d070f82011-02-26 10:20:36 +01002222 wlan = asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_WLAN);
Corentin Charye12e6d92011-02-26 10:20:31 +01002223 asus_wmi_set_devstate(ASUS_WMI_DEVID_WLAN, wlan, NULL);
Corentin Chary0773d7f2011-02-06 13:28:32 +01002224 }
2225
2226 return 0;
2227}
2228
Oleksij Rempel30734042015-09-14 11:16:30 +02002229static int asus_hotk_resume(struct device *device)
2230{
2231 struct asus_wmi *asus = dev_get_drvdata(device);
2232
2233 if (!IS_ERR_OR_NULL(asus->kbd_led.dev))
Jian-Hong Pan9fe44fc2018-09-27 16:50:09 +08002234 kbd_led_update(asus);
Oleksij Rempel30734042015-09-14 11:16:30 +02002235
Chris Chiu487579b2019-04-18 14:46:48 +08002236 if (asus_wmi_has_fnlock_key(asus))
2237 asus_wmi_fnlock_update(asus);
Oleksij Rempel30734042015-09-14 11:16:30 +02002238 return 0;
2239}
2240
Corentin Charye12e6d92011-02-26 10:20:31 +01002241static int asus_hotk_restore(struct device *device)
Corentin Chary0773d7f2011-02-06 13:28:32 +01002242{
Corentin Charye12e6d92011-02-26 10:20:31 +01002243 struct asus_wmi *asus = dev_get_drvdata(device);
Corentin Chary0773d7f2011-02-06 13:28:32 +01002244 int bl;
2245
2246 /* Refresh both wlan rfkill state and pci hotplug */
Corentin Charya7ce3f02011-02-26 10:20:33 +01002247 if (asus->wlan.rfkill)
Corentin Charye12e6d92011-02-26 10:20:31 +01002248 asus_rfkill_hotplug(asus);
Corentin Chary0773d7f2011-02-06 13:28:32 +01002249
Corentin Charya7ce3f02011-02-26 10:20:33 +01002250 if (asus->bluetooth.rfkill) {
Corentin Chary1d070f82011-02-26 10:20:36 +01002251 bl = !asus_wmi_get_devstate_simple(asus,
2252 ASUS_WMI_DEVID_BLUETOOTH);
Corentin Charya7ce3f02011-02-26 10:20:33 +01002253 rfkill_set_sw_state(asus->bluetooth.rfkill, bl);
Corentin Chary2e9e1592011-02-06 13:28:37 +01002254 }
Corentin Charya7ce3f02011-02-26 10:20:33 +01002255 if (asus->wimax.rfkill) {
Corentin Chary1d070f82011-02-26 10:20:36 +01002256 bl = !asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_WIMAX);
Corentin Charya7ce3f02011-02-26 10:20:33 +01002257 rfkill_set_sw_state(asus->wimax.rfkill, bl);
Corentin Chary2e9e1592011-02-06 13:28:37 +01002258 }
Corentin Charya7ce3f02011-02-26 10:20:33 +01002259 if (asus->wwan3g.rfkill) {
Corentin Chary1d070f82011-02-26 10:20:36 +01002260 bl = !asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_WWAN3G);
Corentin Charya7ce3f02011-02-26 10:20:33 +01002261 rfkill_set_sw_state(asus->wwan3g.rfkill, bl);
Corentin Chary0773d7f2011-02-06 13:28:32 +01002262 }
Corentin Chary43be8bd2011-07-01 11:34:40 +02002263 if (asus->gps.rfkill) {
2264 bl = !asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_GPS);
2265 rfkill_set_sw_state(asus->gps.rfkill, bl);
2266 }
Corentin Charya912d322011-07-01 11:34:41 +02002267 if (asus->uwb.rfkill) {
2268 bl = !asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_UWB);
2269 rfkill_set_sw_state(asus->uwb.rfkill, bl);
2270 }
Oleksij Rempel30734042015-09-14 11:16:30 +02002271 if (!IS_ERR_OR_NULL(asus->kbd_led.dev))
Jian-Hong Pan9fe44fc2018-09-27 16:50:09 +08002272 kbd_led_update(asus);
Corentin Chary0773d7f2011-02-06 13:28:32 +01002273
Chris Chiu487579b2019-04-18 14:46:48 +08002274 if (asus_wmi_has_fnlock_key(asus))
2275 asus_wmi_fnlock_update(asus);
Corentin Chary0773d7f2011-02-06 13:28:32 +01002276 return 0;
2277}
2278
Corentin Charye12e6d92011-02-26 10:20:31 +01002279static const struct dev_pm_ops asus_pm_ops = {
2280 .thaw = asus_hotk_thaw,
2281 .restore = asus_hotk_restore,
Oleksij Rempel30734042015-09-14 11:16:30 +02002282 .resume = asus_hotk_resume,
Corentin Chary0773d7f2011-02-06 13:28:32 +01002283};
2284
Corentin Charye12e6d92011-02-26 10:20:31 +01002285static int asus_wmi_probe(struct platform_device *pdev)
Corentin Charyd358cb52010-11-29 08:14:14 +01002286{
Corentin Charye12e6d92011-02-26 10:20:31 +01002287 struct platform_driver *pdrv = to_platform_driver(pdev->dev.driver);
2288 struct asus_wmi_driver *wdrv = to_asus_wmi_driver(pdrv);
2289 int ret;
Corentin Charyd358cb52010-11-29 08:14:14 +01002290
Corentin Charye12e6d92011-02-26 10:20:31 +01002291 if (!wmi_has_guid(ASUS_WMI_MGMT_GUID)) {
Hans de Goedec9946112019-01-21 14:24:36 +01002292 pr_warn("ASUS Management GUID not found\n");
Yong Wangee027e42010-03-21 10:26:34 +08002293 return -ENODEV;
2294 }
2295
Corentin Charye12e6d92011-02-26 10:20:31 +01002296 if (wdrv->event_guid && !wmi_has_guid(wdrv->event_guid)) {
Hans de Goedec9946112019-01-21 14:24:36 +01002297 pr_warn("ASUS Event GUID not found\n");
Corentin Charyd358cb52010-11-29 08:14:14 +01002298 return -ENODEV;
2299 }
2300
Corentin Charye12e6d92011-02-26 10:20:31 +01002301 if (wdrv->probe) {
2302 ret = wdrv->probe(pdev);
2303 if (ret)
2304 return ret;
2305 }
2306
2307 return asus_wmi_add(pdev);
Corentin Charya04ce292011-02-06 13:28:33 +01002308}
Yong Wangee027e42010-03-21 10:26:34 +08002309
Corentin Charye12e6d92011-02-26 10:20:31 +01002310static bool used;
Yong Wangee027e42010-03-21 10:26:34 +08002311
Corentin Chary8fe8c252011-07-01 11:34:32 +02002312int __init_or_module asus_wmi_register_driver(struct asus_wmi_driver *driver)
Corentin Charya04ce292011-02-06 13:28:33 +01002313{
Corentin Charye12e6d92011-02-26 10:20:31 +01002314 struct platform_driver *platform_driver;
2315 struct platform_device *platform_device;
2316
2317 if (used)
2318 return -EBUSY;
2319
2320 platform_driver = &driver->platform_driver;
2321 platform_driver->remove = asus_wmi_remove;
2322 platform_driver->driver.owner = driver->owner;
2323 platform_driver->driver.name = driver->name;
2324 platform_driver->driver.pm = &asus_pm_ops;
2325
2326 platform_device = platform_create_bundle(platform_driver,
2327 asus_wmi_probe,
Corentin Charya04ce292011-02-06 13:28:33 +01002328 NULL, 0, NULL, 0);
2329 if (IS_ERR(platform_device))
2330 return PTR_ERR(platform_device);
Corentin Charye12e6d92011-02-26 10:20:31 +01002331
2332 used = true;
2333 return 0;
2334}
2335EXPORT_SYMBOL_GPL(asus_wmi_register_driver);
2336
2337void asus_wmi_unregister_driver(struct asus_wmi_driver *driver)
2338{
2339 platform_device_unregister(driver->platform_device);
2340 platform_driver_unregister(&driver->platform_driver);
2341 used = false;
2342}
2343EXPORT_SYMBOL_GPL(asus_wmi_unregister_driver);
2344
2345static int __init asus_wmi_init(void)
2346{
vic0ed60652013-05-22 21:32:10 +03002347 pr_info("ASUS WMI generic driver loaded\n");
Yong Wangee027e42010-03-21 10:26:34 +08002348 return 0;
2349}
2350
Corentin Charye12e6d92011-02-26 10:20:31 +01002351static void __exit asus_wmi_exit(void)
Yong Wangee027e42010-03-21 10:26:34 +08002352{
vic0ed60652013-05-22 21:32:10 +03002353 pr_info("ASUS WMI generic driver unloaded\n");
Yong Wangee027e42010-03-21 10:26:34 +08002354}
2355
Corentin Charye12e6d92011-02-26 10:20:31 +01002356module_init(asus_wmi_init);
2357module_exit(asus_wmi_exit);