]>
Commit | Line | Data |
---|---|---|
ee027e4a | 1 | /* |
e12e6d94 | 2 | * Asus PC WMI hotkey driver |
ee027e4a YW |
3 | * |
4 | * Copyright(C) 2010 Intel Corporation. | |
57ab7dae | 5 | * Copyright(C) 2010-2011 Corentin Chary <corentin.chary@gmail.com> |
ee027e4a YW |
6 | * |
7 | * Portions based on wistron_btns.c: | |
8 | * Copyright (C) 2005 Miloslav Trmac <mitr@volny.cz> | |
9 | * Copyright (C) 2005 Bernhard Rosenkraenzer <bero@arklinux.org> | |
10 | * Copyright (C) 2005 Dmitry Torokhov <dtor@mail.ru> | |
11 | * | |
12 | * This program is free software; you can redistribute it and/or modify | |
13 | * it under the terms of the GNU General Public License as published by | |
14 | * the Free Software Foundation; either version 2 of the License, or | |
15 | * (at your option) any later version. | |
16 | * | |
17 | * This program is distributed in the hope that it will be useful, | |
18 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
20 | * GNU General Public License for more details. | |
21 | * | |
22 | * You should have received a copy of the GNU General Public License | |
23 | * along with this program; if not, write to the Free Software | |
24 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
25 | */ | |
26 | ||
81248889 YW |
27 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
28 | ||
ee027e4a YW |
29 | #include <linux/kernel.h> |
30 | #include <linux/module.h> | |
31 | #include <linux/init.h> | |
32 | #include <linux/types.h> | |
a32f3926 | 33 | #include <linux/slab.h> |
ee027e4a YW |
34 | #include <linux/input.h> |
35 | #include <linux/input/sparse-keymap.h> | |
3d7b1655 YW |
36 | #include <linux/fb.h> |
37 | #include <linux/backlight.h> | |
084fca63 | 38 | #include <linux/leds.h> |
ba48fdb9 | 39 | #include <linux/rfkill.h> |
afa7c886 CC |
40 | #include <linux/pci.h> |
41 | #include <linux/pci_hotplug.h> | |
e07babde CC |
42 | #include <linux/hwmon.h> |
43 | #include <linux/hwmon-sysfs.h> | |
8c1b2d83 CC |
44 | #include <linux/debugfs.h> |
45 | #include <linux/seq_file.h> | |
45f2c693 | 46 | #include <linux/platform_device.h> |
6118b8ad | 47 | #include <linux/thermal.h> |
8b48463f | 48 | #include <linux/acpi.h> |
d8c66f62 | 49 | #include <linux/dmi.h> |
272c77d5 | 50 | #include <acpi/video.h> |
ee027e4a | 51 | |
e12e6d94 | 52 | #include "asus-wmi.h" |
45f2c693 | 53 | |
5909c654 | 54 | MODULE_AUTHOR("Corentin Chary <corentin.chary@gmail.com>, " |
e12e6d94 CC |
55 | "Yong Wang <yong.y.wang@intel.com>"); |
56 | MODULE_DESCRIPTION("Asus Generic WMI Driver"); | |
ee027e4a YW |
57 | MODULE_LICENSE("GPL"); |
58 | ||
e12e6d94 CC |
59 | #define to_asus_wmi_driver(pdrv) \ |
60 | (container_of((pdrv), struct asus_wmi_driver, platform_driver)) | |
ee027e4a | 61 | |
e12e6d94 | 62 | #define ASUS_WMI_MGMT_GUID "97845ED0-4E6D-11DE-8A39-0800200C9A66" |
ee027e4a | 63 | |
33e0e6fe CC |
64 | #define NOTIFY_BRNUP_MIN 0x11 |
65 | #define NOTIFY_BRNUP_MAX 0x1f | |
66 | #define NOTIFY_BRNDOWN_MIN 0x20 | |
67 | #define NOTIFY_BRNDOWN_MAX 0x2e | |
e9809c0b CC |
68 | #define NOTIFY_KBD_BRTUP 0xc4 |
69 | #define NOTIFY_KBD_BRTDWN 0xc5 | |
ee027e4a | 70 | |
43815941 | 71 | /* WMI Methods */ |
2f686b54 CC |
72 | #define ASUS_WMI_METHODID_SPEC 0x43455053 /* BIOS SPECification */ |
73 | #define ASUS_WMI_METHODID_SFBD 0x44424653 /* Set First Boot Device */ | |
74 | #define ASUS_WMI_METHODID_GLCD 0x44434C47 /* Get LCD status */ | |
75 | #define ASUS_WMI_METHODID_GPID 0x44495047 /* Get Panel ID?? (Resol) */ | |
76 | #define ASUS_WMI_METHODID_QMOD 0x444F4D51 /* Quiet MODe */ | |
77 | #define ASUS_WMI_METHODID_SPLV 0x4C425053 /* Set Panel Light Value */ | |
53e755c2 | 78 | #define ASUS_WMI_METHODID_AGFN 0x4E464741 /* FaN? */ |
2f686b54 CC |
79 | #define ASUS_WMI_METHODID_SFUN 0x4E554653 /* FUNCtionalities */ |
80 | #define ASUS_WMI_METHODID_SDSP 0x50534453 /* Set DiSPlay output */ | |
81 | #define ASUS_WMI_METHODID_GDSP 0x50534447 /* Get DiSPlay output */ | |
82 | #define ASUS_WMI_METHODID_DEVP 0x50564544 /* DEVice Policy */ | |
83 | #define ASUS_WMI_METHODID_OSVR 0x5256534F /* OS VeRsion */ | |
84 | #define ASUS_WMI_METHODID_DSTS 0x53544344 /* Device STatuS */ | |
85 | #define ASUS_WMI_METHODID_DSTS2 0x53545344 /* Device STatuS #2*/ | |
86 | #define ASUS_WMI_METHODID_BSTS 0x53545342 /* Bios STatuS ? */ | |
87 | #define ASUS_WMI_METHODID_DEVS 0x53564544 /* DEVice Set */ | |
88 | #define ASUS_WMI_METHODID_CFVS 0x53564643 /* CPU Frequency Volt Set */ | |
89 | #define ASUS_WMI_METHODID_KBFT 0x5446424B /* KeyBoard FilTer */ | |
90 | #define ASUS_WMI_METHODID_INIT 0x54494E49 /* INITialize */ | |
91 | #define ASUS_WMI_METHODID_HKEY 0x59454B48 /* Hot KEY ?? */ | |
3d7b1655 | 92 | |
d33da3b6 CC |
93 | #define ASUS_WMI_UNSUPPORTED_METHOD 0xFFFFFFFE |
94 | ||
43815941 | 95 | /* Wireless */ |
2f686b54 CC |
96 | #define ASUS_WMI_DEVID_HW_SWITCH 0x00010001 |
97 | #define ASUS_WMI_DEVID_WIRELESS_LED 0x00010002 | |
79ec1172 | 98 | #define ASUS_WMI_DEVID_CWAP 0x00010003 |
e12e6d94 | 99 | #define ASUS_WMI_DEVID_WLAN 0x00010011 |
a50bd128 | 100 | #define ASUS_WMI_DEVID_WLAN_LED 0x00010012 |
e12e6d94 | 101 | #define ASUS_WMI_DEVID_BLUETOOTH 0x00010013 |
2f686b54 | 102 | #define ASUS_WMI_DEVID_GPS 0x00010015 |
e12e6d94 CC |
103 | #define ASUS_WMI_DEVID_WIMAX 0x00010017 |
104 | #define ASUS_WMI_DEVID_WWAN3G 0x00010019 | |
2f686b54 CC |
105 | #define ASUS_WMI_DEVID_UWB 0x00010021 |
106 | ||
107 | /* Leds */ | |
108 | /* 0x000200XX and 0x000400XX */ | |
79ec1172 CC |
109 | #define ASUS_WMI_DEVID_LED1 0x00020011 |
110 | #define ASUS_WMI_DEVID_LED2 0x00020012 | |
111 | #define ASUS_WMI_DEVID_LED3 0x00020013 | |
112 | #define ASUS_WMI_DEVID_LED4 0x00020014 | |
113 | #define ASUS_WMI_DEVID_LED5 0x00020015 | |
114 | #define ASUS_WMI_DEVID_LED6 0x00020016 | |
43815941 CC |
115 | |
116 | /* Backlight and Brightness */ | |
aca234f6 | 117 | #define ASUS_WMI_DEVID_ALS_ENABLE 0x00050001 /* Ambient Light Sensor */ |
e12e6d94 CC |
118 | #define ASUS_WMI_DEVID_BACKLIGHT 0x00050011 |
119 | #define ASUS_WMI_DEVID_BRIGHTNESS 0x00050012 | |
2f686b54 CC |
120 | #define ASUS_WMI_DEVID_KBD_BACKLIGHT 0x00050021 |
121 | #define ASUS_WMI_DEVID_LIGHT_SENSOR 0x00050022 /* ?? */ | |
43815941 CC |
122 | |
123 | /* Misc */ | |
e12e6d94 | 124 | #define ASUS_WMI_DEVID_CAMERA 0x00060013 |
43815941 CC |
125 | |
126 | /* Storage */ | |
e12e6d94 | 127 | #define ASUS_WMI_DEVID_CARDREADER 0x00080013 |
43815941 CC |
128 | |
129 | /* Input */ | |
57ab7dae | 130 | #define ASUS_WMI_DEVID_TOUCHPAD 0x00100011 |
e12e6d94 | 131 | #define ASUS_WMI_DEVID_TOUCHPAD_LED 0x00100012 |
3d7b1655 | 132 | |
2f686b54 CC |
133 | /* Fan, Thermal */ |
134 | #define ASUS_WMI_DEVID_THERMAL_CTRL 0x00110011 | |
135 | #define ASUS_WMI_DEVID_FAN_CTRL 0x00110012 | |
136 | ||
137 | /* Power */ | |
138 | #define ASUS_WMI_DEVID_PROCESSOR_STATE 0x00120012 | |
139 | ||
c0b91b6d AK |
140 | /* Deep S3 / Resume on LID open */ |
141 | #define ASUS_WMI_DEVID_LID_RESUME 0x00120031 | |
142 | ||
43815941 | 143 | /* DSTS masks */ |
e12e6d94 | 144 | #define ASUS_WMI_DSTS_STATUS_BIT 0x00000001 |
a75fe0d7 | 145 | #define ASUS_WMI_DSTS_UNKNOWN_BIT 0x00000002 |
e12e6d94 | 146 | #define ASUS_WMI_DSTS_PRESENCE_BIT 0x00010000 |
2f686b54 CC |
147 | #define ASUS_WMI_DSTS_USER_BIT 0x00020000 |
148 | #define ASUS_WMI_DSTS_BIOS_BIT 0x00040000 | |
e12e6d94 CC |
149 | #define ASUS_WMI_DSTS_BRIGHTNESS_MASK 0x000000FF |
150 | #define ASUS_WMI_DSTS_MAX_BRIGTH_MASK 0x0000FF00 | |
ee027e4a | 151 | |
53e755c2 KB |
152 | #define ASUS_FAN_DESC "cpu_fan" |
153 | #define ASUS_FAN_MFUN 0x13 | |
154 | #define ASUS_FAN_SFUN_READ 0x06 | |
155 | #define ASUS_FAN_SFUN_WRITE 0x07 | |
156 | #define ASUS_FAN_CTRL_MANUAL 1 | |
157 | #define ASUS_FAN_CTRL_AUTO 2 | |
158 | ||
8023eff1 KCH |
159 | #define USB_INTEL_XUSB2PR 0xD0 |
160 | #define PCI_DEVICE_ID_INTEL_LYNXPOINT_LP_XHCI 0x9c31 | |
161 | ||
71050ae7 JPRV |
162 | static const char * const ashs_ids[] = { "ATK4001", "ATK4002", NULL }; |
163 | ||
3d7b1655 | 164 | struct bios_args { |
d33da3b6 CC |
165 | u32 arg0; |
166 | u32 arg1; | |
167 | } __packed; | |
3d7b1655 | 168 | |
53e755c2 KB |
169 | /* |
170 | * Struct that's used for all methods called via AGFN. Naming is | |
171 | * identically to the AML code. | |
172 | */ | |
173 | struct agfn_args { | |
174 | u16 mfun; /* probably "Multi-function" to be called */ | |
175 | u16 sfun; /* probably "Sub-function" to be called */ | |
176 | u16 len; /* size of the hole struct, including subfunction fields */ | |
177 | u8 stas; /* not used by now */ | |
178 | u8 err; /* zero on success */ | |
179 | } __packed; | |
180 | ||
181 | /* struct used for calling fan read and write methods */ | |
182 | struct fan_args { | |
183 | struct agfn_args agfn; /* common fields */ | |
184 | u8 fan; /* fan number: 0: set auto mode 1: 1st fan */ | |
185 | u32 speed; /* read: RPM/100 - write: 0-255 */ | |
186 | } __packed; | |
187 | ||
8c1b2d83 | 188 | /* |
e12e6d94 | 189 | * <platform>/ - debugfs root directory |
8c1b2d83 CC |
190 | * dev_id - current dev_id |
191 | * ctrl_param - current ctrl_param | |
ef343491 | 192 | * method_id - current method_id |
8c1b2d83 CC |
193 | * devs - call DEVS(dev_id, ctrl_param) and print result |
194 | * dsts - call DSTS(dev_id) and print result | |
ef343491 | 195 | * call - call method_id(dev_id, ctrl_param) and print result |
8c1b2d83 | 196 | */ |
e12e6d94 | 197 | struct asus_wmi_debug { |
8c1b2d83 | 198 | struct dentry *root; |
ef343491 | 199 | u32 method_id; |
8c1b2d83 CC |
200 | u32 dev_id; |
201 | u32 ctrl_param; | |
202 | }; | |
203 | ||
a7ce3f04 CC |
204 | struct asus_rfkill { |
205 | struct asus_wmi *asus; | |
206 | struct rfkill *rfkill; | |
207 | u32 dev_id; | |
208 | }; | |
209 | ||
e12e6d94 | 210 | struct asus_wmi { |
1d070f89 | 211 | int dsts_id; |
46dbca87 CC |
212 | int spec; |
213 | int sfun; | |
1d070f89 | 214 | |
81248889 | 215 | struct input_dev *inputdev; |
3d7b1655 | 216 | struct backlight_device *backlight_device; |
27c136c8 | 217 | struct platform_device *platform_device; |
084fca63 | 218 | |
6cae06e6 AK |
219 | struct led_classdev wlan_led; |
220 | int wlan_led_wk; | |
084fca63 CC |
221 | struct led_classdev tpd_led; |
222 | int tpd_led_wk; | |
e9809c0b CC |
223 | struct led_classdev kbd_led; |
224 | int kbd_led_wk; | |
084fca63 CC |
225 | struct workqueue_struct *led_workqueue; |
226 | struct work_struct tpd_led_work; | |
e9809c0b | 227 | struct work_struct kbd_led_work; |
6cae06e6 | 228 | struct work_struct wlan_led_work; |
ba48fdb9 | 229 | |
a7ce3f04 CC |
230 | struct asus_rfkill wlan; |
231 | struct asus_rfkill bluetooth; | |
232 | struct asus_rfkill wimax; | |
233 | struct asus_rfkill wwan3g; | |
43be8bde | 234 | struct asus_rfkill gps; |
a912d329 | 235 | struct asus_rfkill uwb; |
8c1b2d83 | 236 | |
53e755c2 KB |
237 | bool asus_hwmon_fan_manual_mode; |
238 | int asus_hwmon_num_fans; | |
239 | int asus_hwmon_pwm; | |
240 | ||
afa7c886 CC |
241 | struct hotplug_slot *hotplug_slot; |
242 | struct mutex hotplug_lock; | |
279f8f95 CC |
243 | struct mutex wmi_lock; |
244 | struct workqueue_struct *hotplug_workqueue; | |
245 | struct work_struct hotplug_work; | |
afa7c886 | 246 | |
e12e6d94 CC |
247 | struct asus_wmi_debug debug; |
248 | ||
249 | struct asus_wmi_driver *driver; | |
81248889 YW |
250 | }; |
251 | ||
e12e6d94 | 252 | static int asus_wmi_input_init(struct asus_wmi *asus) |
ee027e4a YW |
253 | { |
254 | int err; | |
255 | ||
e12e6d94 CC |
256 | asus->inputdev = input_allocate_device(); |
257 | if (!asus->inputdev) | |
ee027e4a YW |
258 | return -ENOMEM; |
259 | ||
58a9f397 CC |
260 | asus->inputdev->name = asus->driver->input_name; |
261 | asus->inputdev->phys = asus->driver->input_phys; | |
e12e6d94 CC |
262 | asus->inputdev->id.bustype = BUS_HOST; |
263 | asus->inputdev->dev.parent = &asus->platform_device->dev; | |
39bbde05 | 264 | set_bit(EV_REP, asus->inputdev->evbit); |
ee027e4a | 265 | |
e12e6d94 | 266 | err = sparse_keymap_setup(asus->inputdev, asus->driver->keymap, NULL); |
ee027e4a YW |
267 | if (err) |
268 | goto err_free_dev; | |
269 | ||
e12e6d94 | 270 | err = input_register_device(asus->inputdev); |
ee027e4a | 271 | if (err) |
c2ef3a1c | 272 | goto err_free_dev; |
ee027e4a YW |
273 | |
274 | return 0; | |
275 | ||
ee027e4a | 276 | err_free_dev: |
e12e6d94 | 277 | input_free_device(asus->inputdev); |
ee027e4a YW |
278 | return err; |
279 | } | |
280 | ||
e12e6d94 | 281 | static void asus_wmi_input_exit(struct asus_wmi *asus) |
81248889 | 282 | { |
c2ef3a1c | 283 | if (asus->inputdev) |
e12e6d94 | 284 | input_unregister_device(asus->inputdev); |
81248889 | 285 | |
e12e6d94 | 286 | asus->inputdev = NULL; |
81248889 YW |
287 | } |
288 | ||
d33da3b6 CC |
289 | static int asus_wmi_evaluate_method(u32 method_id, u32 arg0, u32 arg1, |
290 | u32 *retval) | |
3d7b1655 | 291 | { |
d33da3b6 CC |
292 | struct bios_args args = { |
293 | .arg0 = arg0, | |
294 | .arg1 = arg1, | |
295 | }; | |
296 | struct acpi_buffer input = { (acpi_size) sizeof(args), &args }; | |
3d7b1655 | 297 | struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER, NULL }; |
3d7b1655 | 298 | acpi_status status; |
d33da3b6 | 299 | union acpi_object *obj; |
8ad3be1e | 300 | u32 tmp = 0; |
3d7b1655 | 301 | |
d33da3b6 | 302 | status = wmi_evaluate_method(ASUS_WMI_MGMT_GUID, 1, method_id, |
afa7c886 | 303 | &input, &output); |
3d7b1655 YW |
304 | |
305 | if (ACPI_FAILURE(status)) | |
d33da3b6 | 306 | goto exit; |
3d7b1655 YW |
307 | |
308 | obj = (union acpi_object *)output.pointer; | |
309 | if (obj && obj->type == ACPI_TYPE_INTEGER) | |
e12e6d94 | 310 | tmp = (u32) obj->integer.value; |
3d7b1655 | 311 | |
2a3f0064 CC |
312 | if (retval) |
313 | *retval = tmp; | |
3d7b1655 YW |
314 | |
315 | kfree(obj); | |
316 | ||
d33da3b6 CC |
317 | exit: |
318 | if (ACPI_FAILURE(status)) | |
319 | return -EIO; | |
320 | ||
321 | if (tmp == ASUS_WMI_UNSUPPORTED_METHOD) | |
322 | return -ENODEV; | |
3d7b1655 | 323 | |
d33da3b6 | 324 | return 0; |
3d7b1655 YW |
325 | } |
326 | ||
53e755c2 KB |
327 | static int asus_wmi_evaluate_method_agfn(const struct acpi_buffer args) |
328 | { | |
329 | struct acpi_buffer input; | |
330 | u64 phys_addr; | |
331 | u32 retval; | |
332 | u32 status = -1; | |
333 | ||
334 | /* | |
335 | * Copy to dma capable address otherwise memory corruption occurs as | |
336 | * bios has to be able to access it. | |
337 | */ | |
338 | input.pointer = kzalloc(args.length, GFP_DMA | GFP_KERNEL); | |
339 | input.length = args.length; | |
340 | if (!input.pointer) | |
341 | return -ENOMEM; | |
342 | phys_addr = virt_to_phys(input.pointer); | |
343 | memcpy(input.pointer, args.pointer, args.length); | |
344 | ||
345 | status = asus_wmi_evaluate_method(ASUS_WMI_METHODID_AGFN, | |
346 | phys_addr, 0, &retval); | |
347 | if (!status) | |
348 | memcpy(args.pointer, input.pointer, args.length); | |
349 | ||
350 | kfree(input.pointer); | |
351 | if (status) | |
352 | return -ENXIO; | |
353 | ||
354 | return retval; | |
355 | } | |
356 | ||
1d070f89 | 357 | static int asus_wmi_get_devstate(struct asus_wmi *asus, u32 dev_id, u32 *retval) |
3d7b1655 | 358 | { |
1d070f89 | 359 | return asus_wmi_evaluate_method(asus->dsts_id, dev_id, 0, retval); |
d33da3b6 | 360 | } |
3d7b1655 | 361 | |
d33da3b6 | 362 | static int asus_wmi_set_devstate(u32 dev_id, u32 ctrl_param, |
1d070f89 | 363 | u32 *retval) |
d33da3b6 CC |
364 | { |
365 | return asus_wmi_evaluate_method(ASUS_WMI_METHODID_DEVS, dev_id, | |
366 | ctrl_param, retval); | |
3d7b1655 YW |
367 | } |
368 | ||
5c95638d | 369 | /* Helper for special devices with magic return codes */ |
1d070f89 CC |
370 | static int asus_wmi_get_devstate_bits(struct asus_wmi *asus, |
371 | u32 dev_id, u32 mask) | |
5c95638d CC |
372 | { |
373 | u32 retval = 0; | |
d33da3b6 | 374 | int err; |
5c95638d | 375 | |
1d070f89 | 376 | err = asus_wmi_get_devstate(asus, dev_id, &retval); |
5c95638d | 377 | |
d33da3b6 CC |
378 | if (err < 0) |
379 | return err; | |
5c95638d | 380 | |
e12e6d94 | 381 | if (!(retval & ASUS_WMI_DSTS_PRESENCE_BIT)) |
5c95638d CC |
382 | return -ENODEV; |
383 | ||
a75fe0d7 CC |
384 | if (mask == ASUS_WMI_DSTS_STATUS_BIT) { |
385 | if (retval & ASUS_WMI_DSTS_UNKNOWN_BIT) | |
386 | return -ENODEV; | |
387 | } | |
388 | ||
b7187265 CC |
389 | return retval & mask; |
390 | } | |
391 | ||
1d070f89 | 392 | static int asus_wmi_get_devstate_simple(struct asus_wmi *asus, u32 dev_id) |
b7187265 | 393 | { |
1d070f89 CC |
394 | return asus_wmi_get_devstate_bits(asus, dev_id, |
395 | ASUS_WMI_DSTS_STATUS_BIT); | |
5c95638d CC |
396 | } |
397 | ||
084fca63 CC |
398 | /* |
399 | * LEDs | |
400 | */ | |
401 | /* | |
402 | * These functions actually update the LED's, and are called from a | |
403 | * workqueue. By doing this as separate work rather than when the LED | |
e12e6d94 | 404 | * subsystem asks, we avoid messing with the Asus ACPI stuff during a |
084fca63 CC |
405 | * potentially bad time, such as a timer interrupt. |
406 | */ | |
407 | static void tpd_led_update(struct work_struct *work) | |
408 | { | |
409 | int ctrl_param; | |
e12e6d94 | 410 | struct asus_wmi *asus; |
084fca63 | 411 | |
e12e6d94 | 412 | asus = container_of(work, struct asus_wmi, tpd_led_work); |
084fca63 | 413 | |
e12e6d94 CC |
414 | ctrl_param = asus->tpd_led_wk; |
415 | asus_wmi_set_devstate(ASUS_WMI_DEVID_TOUCHPAD_LED, ctrl_param, NULL); | |
084fca63 CC |
416 | } |
417 | ||
418 | static void tpd_led_set(struct led_classdev *led_cdev, | |
419 | enum led_brightness value) | |
420 | { | |
e12e6d94 | 421 | struct asus_wmi *asus; |
084fca63 | 422 | |
e12e6d94 | 423 | asus = container_of(led_cdev, struct asus_wmi, tpd_led); |
084fca63 | 424 | |
e12e6d94 CC |
425 | asus->tpd_led_wk = !!value; |
426 | queue_work(asus->led_workqueue, &asus->tpd_led_work); | |
084fca63 CC |
427 | } |
428 | ||
e12e6d94 | 429 | static int read_tpd_led_state(struct asus_wmi *asus) |
084fca63 | 430 | { |
1d070f89 | 431 | return asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_TOUCHPAD_LED); |
084fca63 CC |
432 | } |
433 | ||
434 | static enum led_brightness tpd_led_get(struct led_classdev *led_cdev) | |
435 | { | |
e12e6d94 | 436 | struct asus_wmi *asus; |
084fca63 | 437 | |
e12e6d94 | 438 | asus = container_of(led_cdev, struct asus_wmi, tpd_led); |
084fca63 | 439 | |
e12e6d94 | 440 | return read_tpd_led_state(asus); |
084fca63 CC |
441 | } |
442 | ||
e9809c0b | 443 | static void kbd_led_update(struct work_struct *work) |
084fca63 | 444 | { |
e9809c0b CC |
445 | int ctrl_param = 0; |
446 | struct asus_wmi *asus; | |
084fca63 | 447 | |
e9809c0b | 448 | asus = container_of(work, struct asus_wmi, kbd_led_work); |
084fca63 | 449 | |
e9809c0b CC |
450 | /* |
451 | * bits 0-2: level | |
452 | * bit 7: light on/off | |
453 | */ | |
454 | if (asus->kbd_led_wk > 0) | |
455 | ctrl_param = 0x80 | (asus->kbd_led_wk & 0x7F); | |
084fca63 | 456 | |
e9809c0b CC |
457 | asus_wmi_set_devstate(ASUS_WMI_DEVID_KBD_BACKLIGHT, ctrl_param, NULL); |
458 | } | |
084fca63 | 459 | |
e9809c0b CC |
460 | static int kbd_led_read(struct asus_wmi *asus, int *level, int *env) |
461 | { | |
462 | int retval; | |
463 | ||
464 | /* | |
465 | * bits 0-2: level | |
466 | * bit 7: light on/off | |
467 | * bit 8-10: environment (0: dark, 1: normal, 2: light) | |
468 | * bit 17: status unknown | |
469 | */ | |
470 | retval = asus_wmi_get_devstate_bits(asus, ASUS_WMI_DEVID_KBD_BACKLIGHT, | |
471 | 0xFFFF); | |
472 | ||
af965e97 | 473 | /* Unknown status is considered as off */ |
e9809c0b | 474 | if (retval == 0x8000) |
af965e97 | 475 | retval = 0; |
e9809c0b CC |
476 | |
477 | if (retval >= 0) { | |
478 | if (level) | |
c09b2237 | 479 | *level = retval & 0x7F; |
e9809c0b CC |
480 | if (env) |
481 | *env = (retval >> 8) & 0x7F; | |
482 | retval = 0; | |
084fca63 CC |
483 | } |
484 | ||
e9809c0b CC |
485 | return retval; |
486 | } | |
487 | ||
488 | static void kbd_led_set(struct led_classdev *led_cdev, | |
489 | enum led_brightness value) | |
490 | { | |
491 | struct asus_wmi *asus; | |
492 | ||
493 | asus = container_of(led_cdev, struct asus_wmi, kbd_led); | |
494 | ||
495 | if (value > asus->kbd_led.max_brightness) | |
496 | value = asus->kbd_led.max_brightness; | |
497 | else if (value < 0) | |
498 | value = 0; | |
499 | ||
500 | asus->kbd_led_wk = value; | |
501 | queue_work(asus->led_workqueue, &asus->kbd_led_work); | |
502 | } | |
503 | ||
504 | static enum led_brightness kbd_led_get(struct led_classdev *led_cdev) | |
505 | { | |
506 | struct asus_wmi *asus; | |
507 | int retval, value; | |
508 | ||
509 | asus = container_of(led_cdev, struct asus_wmi, kbd_led); | |
510 | ||
511 | retval = kbd_led_read(asus, &value, NULL); | |
512 | ||
513 | if (retval < 0) | |
514 | return retval; | |
515 | ||
516 | return value; | |
084fca63 CC |
517 | } |
518 | ||
6cae06e6 AK |
519 | static int wlan_led_unknown_state(struct asus_wmi *asus) |
520 | { | |
521 | u32 result; | |
522 | ||
523 | asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_WIRELESS_LED, &result); | |
524 | ||
525 | return result & ASUS_WMI_DSTS_UNKNOWN_BIT; | |
526 | } | |
527 | ||
528 | static int wlan_led_presence(struct asus_wmi *asus) | |
529 | { | |
530 | u32 result; | |
531 | ||
532 | asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_WIRELESS_LED, &result); | |
533 | ||
534 | return result & ASUS_WMI_DSTS_PRESENCE_BIT; | |
535 | } | |
536 | ||
537 | static void wlan_led_update(struct work_struct *work) | |
538 | { | |
539 | int ctrl_param; | |
540 | struct asus_wmi *asus; | |
541 | ||
542 | asus = container_of(work, struct asus_wmi, wlan_led_work); | |
543 | ||
544 | ctrl_param = asus->wlan_led_wk; | |
545 | asus_wmi_set_devstate(ASUS_WMI_DEVID_WIRELESS_LED, ctrl_param, NULL); | |
546 | } | |
547 | ||
548 | static void wlan_led_set(struct led_classdev *led_cdev, | |
549 | enum led_brightness value) | |
550 | { | |
551 | struct asus_wmi *asus; | |
552 | ||
553 | asus = container_of(led_cdev, struct asus_wmi, wlan_led); | |
554 | ||
555 | asus->wlan_led_wk = !!value; | |
556 | queue_work(asus->led_workqueue, &asus->wlan_led_work); | |
557 | } | |
558 | ||
559 | static enum led_brightness wlan_led_get(struct led_classdev *led_cdev) | |
560 | { | |
561 | struct asus_wmi *asus; | |
562 | u32 result; | |
563 | ||
564 | asus = container_of(led_cdev, struct asus_wmi, wlan_led); | |
565 | asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_WIRELESS_LED, &result); | |
566 | ||
567 | return result & ASUS_WMI_DSTS_BRIGHTNESS_MASK; | |
568 | } | |
569 | ||
e12e6d94 | 570 | static void asus_wmi_led_exit(struct asus_wmi *asus) |
084fca63 | 571 | { |
e9298028 AL |
572 | if (!IS_ERR_OR_NULL(asus->kbd_led.dev)) |
573 | led_classdev_unregister(&asus->kbd_led); | |
574 | if (!IS_ERR_OR_NULL(asus->tpd_led.dev)) | |
e12e6d94 | 575 | led_classdev_unregister(&asus->tpd_led); |
6cae06e6 AK |
576 | if (!IS_ERR_OR_NULL(asus->wlan_led.dev)) |
577 | led_classdev_unregister(&asus->wlan_led); | |
e12e6d94 CC |
578 | if (asus->led_workqueue) |
579 | destroy_workqueue(asus->led_workqueue); | |
084fca63 CC |
580 | } |
581 | ||
e9809c0b CC |
582 | static int asus_wmi_led_init(struct asus_wmi *asus) |
583 | { | |
30734049 | 584 | int rv = 0, led_val; |
e9809c0b CC |
585 | |
586 | asus->led_workqueue = create_singlethread_workqueue("led_workqueue"); | |
587 | if (!asus->led_workqueue) | |
588 | return -ENOMEM; | |
589 | ||
590 | if (read_tpd_led_state(asus) >= 0) { | |
591 | INIT_WORK(&asus->tpd_led_work, tpd_led_update); | |
592 | ||
593 | asus->tpd_led.name = "asus::touchpad"; | |
594 | asus->tpd_led.brightness_set = tpd_led_set; | |
595 | asus->tpd_led.brightness_get = tpd_led_get; | |
596 | asus->tpd_led.max_brightness = 1; | |
597 | ||
598 | rv = led_classdev_register(&asus->platform_device->dev, | |
599 | &asus->tpd_led); | |
600 | if (rv) | |
601 | goto error; | |
602 | } | |
603 | ||
30734049 OR |
604 | led_val = kbd_led_read(asus, NULL, NULL); |
605 | if (led_val >= 0) { | |
e9809c0b CC |
606 | INIT_WORK(&asus->kbd_led_work, kbd_led_update); |
607 | ||
30734049 | 608 | asus->kbd_led_wk = led_val; |
e9809c0b CC |
609 | asus->kbd_led.name = "asus::kbd_backlight"; |
610 | asus->kbd_led.brightness_set = kbd_led_set; | |
611 | asus->kbd_led.brightness_get = kbd_led_get; | |
612 | asus->kbd_led.max_brightness = 3; | |
613 | ||
614 | rv = led_classdev_register(&asus->platform_device->dev, | |
615 | &asus->kbd_led); | |
6cae06e6 AK |
616 | if (rv) |
617 | goto error; | |
618 | } | |
619 | ||
f515623c | 620 | if (wlan_led_presence(asus) && (asus->driver->quirks->wapf > 0)) { |
6cae06e6 AK |
621 | INIT_WORK(&asus->wlan_led_work, wlan_led_update); |
622 | ||
623 | asus->wlan_led.name = "asus::wlan"; | |
624 | asus->wlan_led.brightness_set = wlan_led_set; | |
625 | if (!wlan_led_unknown_state(asus)) | |
626 | asus->wlan_led.brightness_get = wlan_led_get; | |
627 | asus->wlan_led.flags = LED_CORE_SUSPENDRESUME; | |
628 | asus->wlan_led.max_brightness = 1; | |
629 | asus->wlan_led.default_trigger = "asus-wlan"; | |
630 | ||
631 | rv = led_classdev_register(&asus->platform_device->dev, | |
632 | &asus->wlan_led); | |
e9809c0b CC |
633 | } |
634 | ||
635 | error: | |
636 | if (rv) | |
637 | asus_wmi_led_exit(asus); | |
638 | ||
639 | return rv; | |
640 | } | |
641 | ||
642 | ||
afa7c886 CC |
643 | /* |
644 | * PCI hotplug (for wlan rfkill) | |
645 | */ | |
e12e6d94 | 646 | static bool asus_wlan_rfkill_blocked(struct asus_wmi *asus) |
afa7c886 | 647 | { |
1d070f89 | 648 | int result = asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_WLAN); |
afa7c886 | 649 | |
5c95638d | 650 | if (result < 0) |
afa7c886 | 651 | return false; |
5c95638d | 652 | return !result; |
afa7c886 CC |
653 | } |
654 | ||
e12e6d94 | 655 | static void asus_rfkill_hotplug(struct asus_wmi *asus) |
afa7c886 CC |
656 | { |
657 | struct pci_dev *dev; | |
658 | struct pci_bus *bus; | |
279f8f95 | 659 | bool blocked; |
afa7c886 CC |
660 | bool absent; |
661 | u32 l; | |
662 | ||
e12e6d94 CC |
663 | mutex_lock(&asus->wmi_lock); |
664 | blocked = asus_wlan_rfkill_blocked(asus); | |
665 | mutex_unlock(&asus->wmi_lock); | |
afa7c886 | 666 | |
e12e6d94 | 667 | mutex_lock(&asus->hotplug_lock); |
8b9ec1da | 668 | pci_lock_rescan_remove(); |
afa7c886 | 669 | |
a7ce3f04 CC |
670 | if (asus->wlan.rfkill) |
671 | rfkill_set_sw_state(asus->wlan.rfkill, blocked); | |
279f8f95 | 672 | |
e12e6d94 | 673 | if (asus->hotplug_slot) { |
afa7c886 CC |
674 | bus = pci_find_bus(0, 1); |
675 | if (!bus) { | |
5ad77dcf | 676 | pr_warn("Unable to find PCI bus 1?\n"); |
afa7c886 CC |
677 | goto out_unlock; |
678 | } | |
679 | ||
680 | if (pci_bus_read_config_dword(bus, 0, PCI_VENDOR_ID, &l)) { | |
681 | pr_err("Unable to read PCI config space?\n"); | |
682 | goto out_unlock; | |
683 | } | |
684 | absent = (l == 0xffffffff); | |
685 | ||
686 | if (blocked != absent) { | |
5ad77dcf JP |
687 | pr_warn("BIOS says wireless lan is %s, " |
688 | "but the pci device is %s\n", | |
689 | blocked ? "blocked" : "unblocked", | |
690 | absent ? "absent" : "present"); | |
691 | pr_warn("skipped wireless hotplug as probably " | |
692 | "inappropriate for this model\n"); | |
afa7c886 CC |
693 | goto out_unlock; |
694 | } | |
695 | ||
696 | if (!blocked) { | |
697 | dev = pci_get_slot(bus, 0); | |
698 | if (dev) { | |
699 | /* Device already present */ | |
700 | pci_dev_put(dev); | |
701 | goto out_unlock; | |
702 | } | |
703 | dev = pci_scan_single_device(bus, 0); | |
704 | if (dev) { | |
705 | pci_bus_assign_resources(bus); | |
c893d133 | 706 | pci_bus_add_device(dev); |
afa7c886 CC |
707 | } |
708 | } else { | |
709 | dev = pci_get_slot(bus, 0); | |
710 | if (dev) { | |
210647af | 711 | pci_stop_and_remove_bus_device(dev); |
afa7c886 CC |
712 | pci_dev_put(dev); |
713 | } | |
714 | } | |
715 | } | |
716 | ||
717 | out_unlock: | |
8b9ec1da | 718 | pci_unlock_rescan_remove(); |
e12e6d94 | 719 | mutex_unlock(&asus->hotplug_lock); |
afa7c886 CC |
720 | } |
721 | ||
e12e6d94 | 722 | static void asus_rfkill_notify(acpi_handle handle, u32 event, void *data) |
afa7c886 | 723 | { |
e12e6d94 | 724 | struct asus_wmi *asus = data; |
afa7c886 CC |
725 | |
726 | if (event != ACPI_NOTIFY_BUS_CHECK) | |
727 | return; | |
728 | ||
279f8f95 | 729 | /* |
e12e6d94 | 730 | * We can't call directly asus_rfkill_hotplug because most |
279f8f95 CC |
731 | * of the time WMBC is still being executed and not reetrant. |
732 | * There is currently no way to tell ACPICA that we want this | |
e12e6d94 | 733 | * method to be serialized, we schedule a asus_rfkill_hotplug |
279f8f95 CC |
734 | * call later, in a safer context. |
735 | */ | |
e12e6d94 | 736 | queue_work(asus->hotplug_workqueue, &asus->hotplug_work); |
afa7c886 CC |
737 | } |
738 | ||
e12e6d94 | 739 | static int asus_register_rfkill_notifier(struct asus_wmi *asus, char *node) |
afa7c886 CC |
740 | { |
741 | acpi_status status; | |
742 | acpi_handle handle; | |
743 | ||
744 | status = acpi_get_handle(NULL, node, &handle); | |
745 | ||
746 | if (ACPI_SUCCESS(status)) { | |
747 | status = acpi_install_notify_handler(handle, | |
748 | ACPI_SYSTEM_NOTIFY, | |
e12e6d94 | 749 | asus_rfkill_notify, asus); |
afa7c886 | 750 | if (ACPI_FAILURE(status)) |
5ad77dcf | 751 | pr_warn("Failed to register notify on %s\n", node); |
afa7c886 CC |
752 | } else |
753 | return -ENODEV; | |
754 | ||
755 | return 0; | |
756 | } | |
757 | ||
e12e6d94 | 758 | static void asus_unregister_rfkill_notifier(struct asus_wmi *asus, char *node) |
afa7c886 CC |
759 | { |
760 | acpi_status status = AE_OK; | |
761 | acpi_handle handle; | |
762 | ||
763 | status = acpi_get_handle(NULL, node, &handle); | |
764 | ||
765 | if (ACPI_SUCCESS(status)) { | |
766 | status = acpi_remove_notify_handler(handle, | |
e12e6d94 CC |
767 | ACPI_SYSTEM_NOTIFY, |
768 | asus_rfkill_notify); | |
afa7c886 CC |
769 | if (ACPI_FAILURE(status)) |
770 | pr_err("Error removing rfkill notify handler %s\n", | |
e12e6d94 | 771 | node); |
afa7c886 CC |
772 | } |
773 | } | |
774 | ||
e12e6d94 CC |
775 | static int asus_get_adapter_status(struct hotplug_slot *hotplug_slot, |
776 | u8 *value) | |
afa7c886 | 777 | { |
1d070f89 CC |
778 | struct asus_wmi *asus = hotplug_slot->private; |
779 | int result = asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_WLAN); | |
afa7c886 | 780 | |
5c95638d CC |
781 | if (result < 0) |
782 | return result; | |
afa7c886 | 783 | |
5c95638d | 784 | *value = !!result; |
afa7c886 CC |
785 | return 0; |
786 | } | |
787 | ||
e12e6d94 | 788 | static void asus_cleanup_pci_hotplug(struct hotplug_slot *hotplug_slot) |
afa7c886 CC |
789 | { |
790 | kfree(hotplug_slot->info); | |
791 | kfree(hotplug_slot); | |
792 | } | |
793 | ||
e12e6d94 | 794 | static struct hotplug_slot_ops asus_hotplug_slot_ops = { |
afa7c886 | 795 | .owner = THIS_MODULE, |
e12e6d94 CC |
796 | .get_adapter_status = asus_get_adapter_status, |
797 | .get_power_status = asus_get_adapter_status, | |
afa7c886 CC |
798 | }; |
799 | ||
e12e6d94 | 800 | static void asus_hotplug_work(struct work_struct *work) |
279f8f95 | 801 | { |
e12e6d94 | 802 | struct asus_wmi *asus; |
279f8f95 | 803 | |
e12e6d94 CC |
804 | asus = container_of(work, struct asus_wmi, hotplug_work); |
805 | asus_rfkill_hotplug(asus); | |
279f8f95 CC |
806 | } |
807 | ||
e12e6d94 | 808 | static int asus_setup_pci_hotplug(struct asus_wmi *asus) |
afa7c886 CC |
809 | { |
810 | int ret = -ENOMEM; | |
811 | struct pci_bus *bus = pci_find_bus(0, 1); | |
812 | ||
813 | if (!bus) { | |
814 | pr_err("Unable to find wifi PCI bus\n"); | |
815 | return -ENODEV; | |
816 | } | |
817 | ||
e12e6d94 CC |
818 | asus->hotplug_workqueue = |
819 | create_singlethread_workqueue("hotplug_workqueue"); | |
820 | if (!asus->hotplug_workqueue) | |
279f8f95 CC |
821 | goto error_workqueue; |
822 | ||
e12e6d94 | 823 | INIT_WORK(&asus->hotplug_work, asus_hotplug_work); |
279f8f95 | 824 | |
e12e6d94 CC |
825 | asus->hotplug_slot = kzalloc(sizeof(struct hotplug_slot), GFP_KERNEL); |
826 | if (!asus->hotplug_slot) | |
afa7c886 CC |
827 | goto error_slot; |
828 | ||
e12e6d94 CC |
829 | asus->hotplug_slot->info = kzalloc(sizeof(struct hotplug_slot_info), |
830 | GFP_KERNEL); | |
831 | if (!asus->hotplug_slot->info) | |
afa7c886 CC |
832 | goto error_info; |
833 | ||
e12e6d94 CC |
834 | asus->hotplug_slot->private = asus; |
835 | asus->hotplug_slot->release = &asus_cleanup_pci_hotplug; | |
836 | asus->hotplug_slot->ops = &asus_hotplug_slot_ops; | |
837 | asus_get_adapter_status(asus->hotplug_slot, | |
838 | &asus->hotplug_slot->info->adapter_status); | |
afa7c886 | 839 | |
e12e6d94 | 840 | ret = pci_hp_register(asus->hotplug_slot, bus, 0, "asus-wifi"); |
afa7c886 CC |
841 | if (ret) { |
842 | pr_err("Unable to register hotplug slot - %d\n", ret); | |
843 | goto error_register; | |
844 | } | |
845 | ||
846 | return 0; | |
847 | ||
848 | error_register: | |
e12e6d94 | 849 | kfree(asus->hotplug_slot->info); |
afa7c886 | 850 | error_info: |
e12e6d94 CC |
851 | kfree(asus->hotplug_slot); |
852 | asus->hotplug_slot = NULL; | |
afa7c886 | 853 | error_slot: |
e12e6d94 | 854 | destroy_workqueue(asus->hotplug_workqueue); |
279f8f95 | 855 | error_workqueue: |
afa7c886 CC |
856 | return ret; |
857 | } | |
858 | ||
ba48fdb9 CC |
859 | /* |
860 | * Rfkill devices | |
861 | */ | |
e12e6d94 | 862 | static int asus_rfkill_set(void *data, bool blocked) |
ba48fdb9 | 863 | { |
a7ce3f04 | 864 | struct asus_rfkill *priv = data; |
ba48fdb9 | 865 | u32 ctrl_param = !blocked; |
a50bd128 | 866 | u32 dev_id = priv->dev_id; |
ba48fdb9 | 867 | |
a50bd128 AK |
868 | /* |
869 | * If the user bit is set, BIOS can't set and record the wlan status, | |
870 | * it will report the value read from id ASUS_WMI_DEVID_WLAN_LED | |
871 | * while we query the wlan status through WMI(ASUS_WMI_DEVID_WLAN). | |
872 | * So, we have to record wlan status in id ASUS_WMI_DEVID_WLAN_LED | |
873 | * while setting the wlan status through WMI. | |
874 | * This is also the behavior that windows app will do. | |
875 | */ | |
876 | if ((dev_id == ASUS_WMI_DEVID_WLAN) && | |
877 | priv->asus->driver->wlan_ctrl_by_user) | |
878 | dev_id = ASUS_WMI_DEVID_WLAN_LED; | |
879 | ||
880 | return asus_wmi_set_devstate(dev_id, ctrl_param, NULL); | |
ba48fdb9 CC |
881 | } |
882 | ||
e12e6d94 | 883 | static void asus_rfkill_query(struct rfkill *rfkill, void *data) |
ba48fdb9 | 884 | { |
a7ce3f04 | 885 | struct asus_rfkill *priv = data; |
5c95638d | 886 | int result; |
ba48fdb9 | 887 | |
1d070f89 | 888 | result = asus_wmi_get_devstate_simple(priv->asus, priv->dev_id); |
ba48fdb9 | 889 | |
5c95638d | 890 | if (result < 0) |
e12e6d94 | 891 | return; |
ba48fdb9 | 892 | |
a7ce3f04 | 893 | rfkill_set_sw_state(priv->rfkill, !result); |
ba48fdb9 CC |
894 | } |
895 | ||
e12e6d94 | 896 | static int asus_rfkill_wlan_set(void *data, bool blocked) |
279f8f95 | 897 | { |
a7ce3f04 CC |
898 | struct asus_rfkill *priv = data; |
899 | struct asus_wmi *asus = priv->asus; | |
279f8f95 CC |
900 | int ret; |
901 | ||
902 | /* | |
903 | * This handler is enabled only if hotplug is enabled. | |
e12e6d94 | 904 | * In this case, the asus_wmi_set_devstate() will |
279f8f95 CC |
905 | * trigger a wmi notification and we need to wait |
906 | * this call to finish before being able to call | |
907 | * any wmi method | |
908 | */ | |
e12e6d94 | 909 | mutex_lock(&asus->wmi_lock); |
a7ce3f04 | 910 | ret = asus_rfkill_set(data, blocked); |
e12e6d94 | 911 | mutex_unlock(&asus->wmi_lock); |
279f8f95 CC |
912 | return ret; |
913 | } | |
914 | ||
e12e6d94 CC |
915 | static const struct rfkill_ops asus_rfkill_wlan_ops = { |
916 | .set_block = asus_rfkill_wlan_set, | |
a7ce3f04 | 917 | .query = asus_rfkill_query, |
279f8f95 CC |
918 | }; |
919 | ||
e12e6d94 CC |
920 | static const struct rfkill_ops asus_rfkill_ops = { |
921 | .set_block = asus_rfkill_set, | |
922 | .query = asus_rfkill_query, | |
ba48fdb9 CC |
923 | }; |
924 | ||
e12e6d94 | 925 | static int asus_new_rfkill(struct asus_wmi *asus, |
a7ce3f04 | 926 | struct asus_rfkill *arfkill, |
e12e6d94 | 927 | const char *name, enum rfkill_type type, int dev_id) |
ba48fdb9 | 928 | { |
1d070f89 | 929 | int result = asus_wmi_get_devstate_simple(asus, dev_id); |
a7ce3f04 | 930 | struct rfkill **rfkill = &arfkill->rfkill; |
ba48fdb9 | 931 | |
5c95638d CC |
932 | if (result < 0) |
933 | return result; | |
ba48fdb9 | 934 | |
a7ce3f04 CC |
935 | arfkill->dev_id = dev_id; |
936 | arfkill->asus = asus; | |
937 | ||
c87992d1 AK |
938 | if (dev_id == ASUS_WMI_DEVID_WLAN && |
939 | asus->driver->quirks->hotplug_wireless) | |
e12e6d94 | 940 | *rfkill = rfkill_alloc(name, &asus->platform_device->dev, type, |
a7ce3f04 | 941 | &asus_rfkill_wlan_ops, arfkill); |
279f8f95 | 942 | else |
e12e6d94 | 943 | *rfkill = rfkill_alloc(name, &asus->platform_device->dev, type, |
a7ce3f04 | 944 | &asus_rfkill_ops, arfkill); |
ba48fdb9 CC |
945 | |
946 | if (!*rfkill) | |
947 | return -EINVAL; | |
948 | ||
e8f56c80 | 949 | if ((dev_id == ASUS_WMI_DEVID_WLAN) && |
f515623c | 950 | (asus->driver->quirks->wapf > 0)) |
6cae06e6 AK |
951 | rfkill_set_led_trigger_name(*rfkill, "asus-wlan"); |
952 | ||
5c95638d | 953 | rfkill_init_sw_state(*rfkill, !result); |
ba48fdb9 CC |
954 | result = rfkill_register(*rfkill); |
955 | if (result) { | |
956 | rfkill_destroy(*rfkill); | |
957 | *rfkill = NULL; | |
958 | return result; | |
959 | } | |
960 | return 0; | |
961 | } | |
962 | ||
e12e6d94 | 963 | static void asus_wmi_rfkill_exit(struct asus_wmi *asus) |
ba48fdb9 | 964 | { |
e12e6d94 CC |
965 | asus_unregister_rfkill_notifier(asus, "\\_SB.PCI0.P0P5"); |
966 | asus_unregister_rfkill_notifier(asus, "\\_SB.PCI0.P0P6"); | |
967 | asus_unregister_rfkill_notifier(asus, "\\_SB.PCI0.P0P7"); | |
a7ce3f04 CC |
968 | if (asus->wlan.rfkill) { |
969 | rfkill_unregister(asus->wlan.rfkill); | |
970 | rfkill_destroy(asus->wlan.rfkill); | |
971 | asus->wlan.rfkill = NULL; | |
ba48fdb9 | 972 | } |
afa7c886 CC |
973 | /* |
974 | * Refresh pci hotplug in case the rfkill state was changed after | |
e12e6d94 | 975 | * asus_unregister_rfkill_notifier() |
afa7c886 | 976 | */ |
e12e6d94 CC |
977 | asus_rfkill_hotplug(asus); |
978 | if (asus->hotplug_slot) | |
979 | pci_hp_deregister(asus->hotplug_slot); | |
980 | if (asus->hotplug_workqueue) | |
981 | destroy_workqueue(asus->hotplug_workqueue); | |
982 | ||
a7ce3f04 CC |
983 | if (asus->bluetooth.rfkill) { |
984 | rfkill_unregister(asus->bluetooth.rfkill); | |
985 | rfkill_destroy(asus->bluetooth.rfkill); | |
986 | asus->bluetooth.rfkill = NULL; | |
ba48fdb9 | 987 | } |
a7ce3f04 CC |
988 | if (asus->wimax.rfkill) { |
989 | rfkill_unregister(asus->wimax.rfkill); | |
990 | rfkill_destroy(asus->wimax.rfkill); | |
991 | asus->wimax.rfkill = NULL; | |
2e9e159d | 992 | } |
a7ce3f04 CC |
993 | if (asus->wwan3g.rfkill) { |
994 | rfkill_unregister(asus->wwan3g.rfkill); | |
995 | rfkill_destroy(asus->wwan3g.rfkill); | |
996 | asus->wwan3g.rfkill = NULL; | |
ba48fdb9 | 997 | } |
43be8bde CC |
998 | if (asus->gps.rfkill) { |
999 | rfkill_unregister(asus->gps.rfkill); | |
1000 | rfkill_destroy(asus->gps.rfkill); | |
1001 | asus->gps.rfkill = NULL; | |
1002 | } | |
a912d329 CC |
1003 | if (asus->uwb.rfkill) { |
1004 | rfkill_unregister(asus->uwb.rfkill); | |
1005 | rfkill_destroy(asus->uwb.rfkill); | |
1006 | asus->uwb.rfkill = NULL; | |
1007 | } | |
ba48fdb9 CC |
1008 | } |
1009 | ||
e12e6d94 | 1010 | static int asus_wmi_rfkill_init(struct asus_wmi *asus) |
ba48fdb9 CC |
1011 | { |
1012 | int result = 0; | |
1013 | ||
e12e6d94 CC |
1014 | mutex_init(&asus->hotplug_lock); |
1015 | mutex_init(&asus->wmi_lock); | |
afa7c886 | 1016 | |
a7ce3f04 CC |
1017 | result = asus_new_rfkill(asus, &asus->wlan, "asus-wlan", |
1018 | RFKILL_TYPE_WLAN, ASUS_WMI_DEVID_WLAN); | |
ba48fdb9 CC |
1019 | |
1020 | if (result && result != -ENODEV) | |
1021 | goto exit; | |
1022 | ||
a7ce3f04 | 1023 | result = asus_new_rfkill(asus, &asus->bluetooth, |
e12e6d94 CC |
1024 | "asus-bluetooth", RFKILL_TYPE_BLUETOOTH, |
1025 | ASUS_WMI_DEVID_BLUETOOTH); | |
ba48fdb9 CC |
1026 | |
1027 | if (result && result != -ENODEV) | |
1028 | goto exit; | |
1029 | ||
a7ce3f04 CC |
1030 | result = asus_new_rfkill(asus, &asus->wimax, "asus-wimax", |
1031 | RFKILL_TYPE_WIMAX, ASUS_WMI_DEVID_WIMAX); | |
2e9e159d CC |
1032 | |
1033 | if (result && result != -ENODEV) | |
1034 | goto exit; | |
1035 | ||
a7ce3f04 CC |
1036 | result = asus_new_rfkill(asus, &asus->wwan3g, "asus-wwan3g", |
1037 | RFKILL_TYPE_WWAN, ASUS_WMI_DEVID_WWAN3G); | |
ba48fdb9 CC |
1038 | |
1039 | if (result && result != -ENODEV) | |
1040 | goto exit; | |
1041 | ||
43be8bde CC |
1042 | result = asus_new_rfkill(asus, &asus->gps, "asus-gps", |
1043 | RFKILL_TYPE_GPS, ASUS_WMI_DEVID_GPS); | |
1044 | ||
1045 | if (result && result != -ENODEV) | |
1046 | goto exit; | |
1047 | ||
a912d329 CC |
1048 | result = asus_new_rfkill(asus, &asus->uwb, "asus-uwb", |
1049 | RFKILL_TYPE_UWB, ASUS_WMI_DEVID_UWB); | |
1050 | ||
1051 | if (result && result != -ENODEV) | |
1052 | goto exit; | |
1053 | ||
c87992d1 | 1054 | if (!asus->driver->quirks->hotplug_wireless) |
c14d4b8e CC |
1055 | goto exit; |
1056 | ||
e12e6d94 | 1057 | result = asus_setup_pci_hotplug(asus); |
afa7c886 CC |
1058 | /* |
1059 | * If we get -EBUSY then something else is handling the PCI hotplug - | |
1060 | * don't fail in this case | |
1061 | */ | |
1062 | if (result == -EBUSY) | |
1063 | result = 0; | |
1064 | ||
e12e6d94 CC |
1065 | asus_register_rfkill_notifier(asus, "\\_SB.PCI0.P0P5"); |
1066 | asus_register_rfkill_notifier(asus, "\\_SB.PCI0.P0P6"); | |
1067 | asus_register_rfkill_notifier(asus, "\\_SB.PCI0.P0P7"); | |
afa7c886 CC |
1068 | /* |
1069 | * Refresh pci hotplug in case the rfkill state was changed during | |
1070 | * setup. | |
1071 | */ | |
e12e6d94 | 1072 | asus_rfkill_hotplug(asus); |
afa7c886 | 1073 | |
ba48fdb9 CC |
1074 | exit: |
1075 | if (result && result != -ENODEV) | |
e12e6d94 | 1076 | asus_wmi_rfkill_exit(asus); |
ba48fdb9 CC |
1077 | |
1078 | if (result == -ENODEV) | |
1079 | result = 0; | |
1080 | ||
1081 | return result; | |
1082 | } | |
1083 | ||
8023eff1 KCH |
1084 | static void asus_wmi_set_xusb2pr(struct asus_wmi *asus) |
1085 | { | |
1086 | struct pci_dev *xhci_pdev; | |
1087 | u32 orig_ports_available; | |
1088 | u32 ports_available = asus->driver->quirks->xusb2pr; | |
1089 | ||
1090 | xhci_pdev = pci_get_device(PCI_VENDOR_ID_INTEL, | |
1091 | PCI_DEVICE_ID_INTEL_LYNXPOINT_LP_XHCI, | |
1092 | NULL); | |
1093 | ||
1094 | if (!xhci_pdev) | |
1095 | return; | |
1096 | ||
1097 | pci_read_config_dword(xhci_pdev, USB_INTEL_XUSB2PR, | |
1098 | &orig_ports_available); | |
1099 | ||
1100 | pci_write_config_dword(xhci_pdev, USB_INTEL_XUSB2PR, | |
1101 | cpu_to_le32(ports_available)); | |
1102 | ||
1103 | pr_info("set USB_INTEL_XUSB2PR old: 0x%04x, new: 0x%04x\n", | |
1104 | orig_ports_available, ports_available); | |
1105 | } | |
1106 | ||
e9b61518 OR |
1107 | /* |
1108 | * Some devices dont support or have borcken get_als method | |
1109 | * but still support set method. | |
1110 | */ | |
1111 | static void asus_wmi_set_als(void) | |
1112 | { | |
1113 | asus_wmi_set_devstate(ASUS_WMI_DEVID_ALS_ENABLE, 1, NULL); | |
1114 | } | |
1115 | ||
e07babde CC |
1116 | /* |
1117 | * Hwmon device | |
1118 | */ | |
53e755c2 KB |
1119 | static int asus_hwmon_agfn_fan_speed_read(struct asus_wmi *asus, int fan, |
1120 | int *speed) | |
1121 | { | |
1122 | struct fan_args args = { | |
1123 | .agfn.len = sizeof(args), | |
1124 | .agfn.mfun = ASUS_FAN_MFUN, | |
1125 | .agfn.sfun = ASUS_FAN_SFUN_READ, | |
1126 | .fan = fan, | |
1127 | .speed = 0, | |
1128 | }; | |
1129 | struct acpi_buffer input = { (acpi_size) sizeof(args), &args }; | |
1130 | int status; | |
1131 | ||
1132 | if (fan != 1) | |
1133 | return -EINVAL; | |
1134 | ||
1135 | status = asus_wmi_evaluate_method_agfn(input); | |
1136 | ||
1137 | if (status || args.agfn.err) | |
1138 | return -ENXIO; | |
1139 | ||
1140 | if (speed) | |
1141 | *speed = args.speed; | |
1142 | ||
1143 | return 0; | |
1144 | } | |
1145 | ||
1146 | static int asus_hwmon_agfn_fan_speed_write(struct asus_wmi *asus, int fan, | |
1147 | int *speed) | |
1148 | { | |
1149 | struct fan_args args = { | |
1150 | .agfn.len = sizeof(args), | |
1151 | .agfn.mfun = ASUS_FAN_MFUN, | |
1152 | .agfn.sfun = ASUS_FAN_SFUN_WRITE, | |
1153 | .fan = fan, | |
1154 | .speed = speed ? *speed : 0, | |
1155 | }; | |
1156 | struct acpi_buffer input = { (acpi_size) sizeof(args), &args }; | |
1157 | int status; | |
1158 | ||
1159 | /* 1: for setting 1st fan's speed 0: setting auto mode */ | |
1160 | if (fan != 1 && fan != 0) | |
1161 | return -EINVAL; | |
1162 | ||
1163 | status = asus_wmi_evaluate_method_agfn(input); | |
1164 | ||
1165 | if (status || args.agfn.err) | |
1166 | return -ENXIO; | |
1167 | ||
1168 | if (speed && fan == 1) | |
1169 | asus->asus_hwmon_pwm = *speed; | |
1170 | ||
1171 | return 0; | |
1172 | } | |
1173 | ||
1174 | /* | |
1175 | * Check if we can read the speed of one fan. If true we assume we can also | |
1176 | * control it. | |
1177 | */ | |
1178 | static int asus_hwmon_get_fan_number(struct asus_wmi *asus, int *num_fans) | |
1179 | { | |
1180 | int status; | |
1181 | int speed = 0; | |
1182 | ||
1183 | *num_fans = 0; | |
1184 | ||
1185 | status = asus_hwmon_agfn_fan_speed_read(asus, 1, &speed); | |
1186 | if (!status) | |
1187 | *num_fans = 1; | |
1188 | ||
1189 | return 0; | |
1190 | } | |
1191 | ||
1192 | static int asus_hwmon_fan_set_auto(struct asus_wmi *asus) | |
1193 | { | |
1194 | int status; | |
1195 | ||
1196 | status = asus_hwmon_agfn_fan_speed_write(asus, 0, NULL); | |
1197 | if (status) | |
1198 | return -ENXIO; | |
1199 | ||
1200 | asus->asus_hwmon_fan_manual_mode = false; | |
1201 | ||
1202 | return 0; | |
1203 | } | |
1204 | ||
1205 | static int asus_hwmon_fan_rpm_show(struct device *dev, int fan) | |
e07babde CC |
1206 | { |
1207 | struct asus_wmi *asus = dev_get_drvdata(dev); | |
53e755c2 KB |
1208 | int value; |
1209 | int ret; | |
1210 | ||
1211 | /* no speed readable on manual mode */ | |
1212 | if (asus->asus_hwmon_fan_manual_mode) | |
1213 | return -ENXIO; | |
1214 | ||
1215 | ret = asus_hwmon_agfn_fan_speed_read(asus, fan+1, &value); | |
1216 | if (ret) { | |
1217 | pr_warn("reading fan speed failed: %d\n", ret); | |
1218 | return -ENXIO; | |
1219 | } | |
1220 | ||
1221 | return value; | |
1222 | } | |
1223 | ||
1224 | static void asus_hwmon_pwm_show(struct asus_wmi *asus, int fan, int *value) | |
1225 | { | |
e07babde CC |
1226 | int err; |
1227 | ||
53e755c2 KB |
1228 | if (asus->asus_hwmon_pwm >= 0) { |
1229 | *value = asus->asus_hwmon_pwm; | |
1230 | return; | |
1231 | } | |
e07babde | 1232 | |
53e755c2 | 1233 | err = asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_FAN_CTRL, value); |
e07babde | 1234 | if (err < 0) |
53e755c2 | 1235 | return; |
e07babde | 1236 | |
53e755c2 KB |
1237 | *value &= 0xFF; |
1238 | ||
1239 | if (*value == 1) /* Low Speed */ | |
1240 | *value = 85; | |
1241 | else if (*value == 2) | |
1242 | *value = 170; | |
1243 | else if (*value == 3) | |
1244 | *value = 255; | |
1245 | else if (*value) { | |
1246 | pr_err("Unknown fan speed %#x\n", *value); | |
1247 | *value = -1; | |
e07babde | 1248 | } |
53e755c2 KB |
1249 | } |
1250 | ||
1251 | static ssize_t pwm1_show(struct device *dev, | |
1252 | struct device_attribute *attr, | |
1253 | char *buf) | |
1254 | { | |
1255 | struct asus_wmi *asus = dev_get_drvdata(dev); | |
1256 | int value; | |
1257 | ||
1258 | asus_hwmon_pwm_show(asus, 0, &value); | |
e07babde CC |
1259 | |
1260 | return sprintf(buf, "%d\n", value); | |
1261 | } | |
1262 | ||
53e755c2 KB |
1263 | static ssize_t pwm1_store(struct device *dev, |
1264 | struct device_attribute *attr, | |
1265 | const char *buf, size_t count) { | |
1266 | struct asus_wmi *asus = dev_get_drvdata(dev); | |
1267 | int value; | |
1268 | int state; | |
1269 | int ret; | |
1270 | ||
1271 | ret = kstrtouint(buf, 10, &value); | |
1272 | ||
1273 | if (ret) | |
1274 | return ret; | |
1275 | ||
1276 | value = clamp(value, 0, 255); | |
1277 | ||
1278 | state = asus_hwmon_agfn_fan_speed_write(asus, 1, &value); | |
1279 | if (state) | |
1280 | pr_warn("Setting fan speed failed: %d\n", state); | |
1281 | else | |
1282 | asus->asus_hwmon_fan_manual_mode = true; | |
1283 | ||
1284 | return count; | |
1285 | } | |
1286 | ||
1287 | static ssize_t fan1_input_show(struct device *dev, | |
1288 | struct device_attribute *attr, | |
1289 | char *buf) | |
1290 | { | |
1291 | int value = asus_hwmon_fan_rpm_show(dev, 0); | |
1292 | ||
1293 | return sprintf(buf, "%d\n", value < 0 ? -1 : value*100); | |
1294 | ||
1295 | } | |
1296 | ||
1297 | static ssize_t pwm1_enable_show(struct device *dev, | |
1298 | struct device_attribute *attr, | |
1299 | char *buf) | |
1300 | { | |
1301 | struct asus_wmi *asus = dev_get_drvdata(dev); | |
1302 | ||
1303 | if (asus->asus_hwmon_fan_manual_mode) | |
1304 | return sprintf(buf, "%d\n", ASUS_FAN_CTRL_MANUAL); | |
1305 | ||
1306 | return sprintf(buf, "%d\n", ASUS_FAN_CTRL_AUTO); | |
1307 | } | |
1308 | ||
1309 | static ssize_t pwm1_enable_store(struct device *dev, | |
1310 | struct device_attribute *attr, | |
1311 | const char *buf, size_t count) | |
1312 | { | |
1313 | struct asus_wmi *asus = dev_get_drvdata(dev); | |
1314 | int status = 0; | |
1315 | int state; | |
1316 | int ret; | |
1317 | ||
1318 | ret = kstrtouint(buf, 10, &state); | |
1319 | ||
1320 | if (ret) | |
1321 | return ret; | |
1322 | ||
1323 | if (state == ASUS_FAN_CTRL_MANUAL) | |
1324 | asus->asus_hwmon_fan_manual_mode = true; | |
1325 | else | |
1326 | status = asus_hwmon_fan_set_auto(asus); | |
1327 | ||
1328 | if (status) | |
1329 | return status; | |
1330 | ||
1331 | return count; | |
1332 | } | |
1333 | ||
1334 | static ssize_t fan1_label_show(struct device *dev, | |
1335 | struct device_attribute *attr, | |
1336 | char *buf) | |
1337 | { | |
1338 | return sprintf(buf, "%s\n", ASUS_FAN_DESC); | |
1339 | } | |
1340 | ||
6118b8ad CC |
1341 | static ssize_t asus_hwmon_temp1(struct device *dev, |
1342 | struct device_attribute *attr, | |
1343 | char *buf) | |
1344 | { | |
1345 | struct asus_wmi *asus = dev_get_drvdata(dev); | |
1346 | u32 value; | |
1347 | int err; | |
1348 | ||
1349 | err = asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_THERMAL_CTRL, &value); | |
1350 | ||
1351 | if (err < 0) | |
1352 | return err; | |
1353 | ||
e866a2e3 | 1354 | value = DECI_KELVIN_TO_CELSIUS((value & 0xFFFF)) * 1000; |
6118b8ad CC |
1355 | |
1356 | return sprintf(buf, "%d\n", value); | |
1357 | } | |
1358 | ||
53e755c2 KB |
1359 | /* Fan1 */ |
1360 | static DEVICE_ATTR_RW(pwm1); | |
1361 | static DEVICE_ATTR_RW(pwm1_enable); | |
1362 | static DEVICE_ATTR_RO(fan1_input); | |
1363 | static DEVICE_ATTR_RO(fan1_label); | |
1364 | ||
1365 | /* Temperature */ | |
50a639fb | 1366 | static DEVICE_ATTR(temp1_input, S_IRUGO, asus_hwmon_temp1, NULL); |
e07babde CC |
1367 | |
1368 | static struct attribute *hwmon_attributes[] = { | |
50a639fb | 1369 | &dev_attr_pwm1.attr, |
53e755c2 KB |
1370 | &dev_attr_pwm1_enable.attr, |
1371 | &dev_attr_fan1_input.attr, | |
1372 | &dev_attr_fan1_label.attr, | |
1373 | ||
50a639fb | 1374 | &dev_attr_temp1_input.attr, |
e07babde CC |
1375 | NULL |
1376 | }; | |
1377 | ||
587a1f16 | 1378 | static umode_t asus_hwmon_sysfs_is_visible(struct kobject *kobj, |
e02431d6 | 1379 | struct attribute *attr, int idx) |
e07babde CC |
1380 | { |
1381 | struct device *dev = container_of(kobj, struct device, kobj); | |
1382 | struct platform_device *pdev = to_platform_device(dev->parent); | |
1383 | struct asus_wmi *asus = platform_get_drvdata(pdev); | |
e07babde | 1384 | int dev_id = -1; |
53e755c2 | 1385 | int fan_attr = -1; |
e07babde | 1386 | u32 value = ASUS_WMI_UNSUPPORTED_METHOD; |
53e755c2 | 1387 | bool ok = true; |
e07babde | 1388 | |
50a639fb | 1389 | if (attr == &dev_attr_pwm1.attr) |
e07babde | 1390 | dev_id = ASUS_WMI_DEVID_FAN_CTRL; |
50a639fb | 1391 | else if (attr == &dev_attr_temp1_input.attr) |
e02431d6 | 1392 | dev_id = ASUS_WMI_DEVID_THERMAL_CTRL; |
e07babde | 1393 | |
53e755c2 KB |
1394 | |
1395 | if (attr == &dev_attr_fan1_input.attr | |
1396 | || attr == &dev_attr_fan1_label.attr | |
1397 | || attr == &dev_attr_pwm1.attr | |
1398 | || attr == &dev_attr_pwm1_enable.attr) { | |
1399 | fan_attr = 1; | |
1400 | } | |
1401 | ||
e07babde CC |
1402 | if (dev_id != -1) { |
1403 | int err = asus_wmi_get_devstate(asus, dev_id, &value); | |
1404 | ||
53e755c2 | 1405 | if (err < 0 && fan_attr == -1) |
e772aed3 | 1406 | return 0; /* can't return negative here */ |
e07babde CC |
1407 | } |
1408 | ||
1409 | if (dev_id == ASUS_WMI_DEVID_FAN_CTRL) { | |
1410 | /* | |
1411 | * We need to find a better way, probably using sfun, | |
1412 | * bits or spec ... | |
1413 | * Currently we disable it if: | |
1414 | * - ASUS_WMI_UNSUPPORTED_METHOD is returned | |
1415 | * - reverved bits are non-zero | |
1416 | * - sfun and presence bit are not set | |
1417 | */ | |
49979d09 | 1418 | if (value == ASUS_WMI_UNSUPPORTED_METHOD || value & 0xFFF80000 |
e07babde CC |
1419 | || (!asus->sfun && !(value & ASUS_WMI_DSTS_PRESENCE_BIT))) |
1420 | ok = false; | |
53e755c2 KB |
1421 | else |
1422 | ok = fan_attr <= asus->asus_hwmon_num_fans; | |
e02431d6 CC |
1423 | } else if (dev_id == ASUS_WMI_DEVID_THERMAL_CTRL) { |
1424 | /* If value is zero, something is clearly wrong */ | |
53e755c2 | 1425 | if (!value) |
e02431d6 | 1426 | ok = false; |
53e755c2 KB |
1427 | } else if (fan_attr <= asus->asus_hwmon_num_fans && fan_attr != -1) { |
1428 | ok = true; | |
1429 | } else { | |
1430 | ok = false; | |
e07babde CC |
1431 | } |
1432 | ||
1433 | return ok ? attr->mode : 0; | |
1434 | } | |
1435 | ||
e90d9ba8 | 1436 | static const struct attribute_group hwmon_attribute_group = { |
e07babde CC |
1437 | .is_visible = asus_hwmon_sysfs_is_visible, |
1438 | .attrs = hwmon_attributes | |
1439 | }; | |
50a639fb | 1440 | __ATTRIBUTE_GROUPS(hwmon_attribute); |
e07babde CC |
1441 | |
1442 | static int asus_wmi_hwmon_init(struct asus_wmi *asus) | |
1443 | { | |
1444 | struct device *hwmon; | |
e07babde | 1445 | |
50a639fb GR |
1446 | hwmon = hwmon_device_register_with_groups(&asus->platform_device->dev, |
1447 | "asus", asus, | |
1448 | hwmon_attribute_groups); | |
e07babde CC |
1449 | if (IS_ERR(hwmon)) { |
1450 | pr_err("Could not register asus hwmon device\n"); | |
1451 | return PTR_ERR(hwmon); | |
1452 | } | |
50a639fb | 1453 | return 0; |
e07babde CC |
1454 | } |
1455 | ||
084fca63 CC |
1456 | /* |
1457 | * Backlight | |
1458 | */ | |
1d070f89 | 1459 | static int read_backlight_power(struct asus_wmi *asus) |
b7187265 | 1460 | { |
6e0044be AK |
1461 | int ret; |
1462 | if (asus->driver->quirks->store_backlight_power) | |
1463 | ret = !asus->driver->panel_power; | |
1464 | else | |
1465 | ret = asus_wmi_get_devstate_simple(asus, | |
1466 | ASUS_WMI_DEVID_BACKLIGHT); | |
b7187265 CC |
1467 | |
1468 | if (ret < 0) | |
1469 | return ret; | |
1470 | ||
1471 | return ret ? FB_BLANK_UNBLANK : FB_BLANK_POWERDOWN; | |
1472 | } | |
1473 | ||
8fbea019 | 1474 | static int read_brightness_max(struct asus_wmi *asus) |
3d7b1655 | 1475 | { |
dfed65d5 | 1476 | u32 retval; |
d33da3b6 | 1477 | int err; |
3d7b1655 | 1478 | |
1d070f89 | 1479 | err = asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_BRIGHTNESS, &retval); |
3d7b1655 | 1480 | |
8fbea019 CC |
1481 | if (err < 0) |
1482 | return err; | |
1483 | ||
1484 | retval = retval & ASUS_WMI_DSTS_MAX_BRIGTH_MASK; | |
1485 | retval >>= 8; | |
1486 | ||
1487 | if (!retval) | |
1488 | return -ENODEV; | |
1489 | ||
1490 | return retval; | |
1491 | } | |
1492 | ||
1493 | static int read_brightness(struct backlight_device *bd) | |
1494 | { | |
1495 | struct asus_wmi *asus = bl_get_data(bd); | |
0986f25f DC |
1496 | u32 retval; |
1497 | int err; | |
8fbea019 CC |
1498 | |
1499 | err = asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_BRIGHTNESS, &retval); | |
1500 | ||
d33da3b6 CC |
1501 | if (err < 0) |
1502 | return err; | |
1503 | ||
1504 | return retval & ASUS_WMI_DSTS_BRIGHTNESS_MASK; | |
3d7b1655 YW |
1505 | } |
1506 | ||
c87992d1 | 1507 | static u32 get_scalar_command(struct backlight_device *bd) |
3d7b1655 | 1508 | { |
1d070f89 | 1509 | struct asus_wmi *asus = bl_get_data(bd); |
c87992d1 | 1510 | u32 ctrl_param = 0; |
3d7b1655 | 1511 | |
c87992d1 AK |
1512 | if ((asus->driver->brightness < bd->props.brightness) || |
1513 | bd->props.brightness == bd->props.max_brightness) | |
1514 | ctrl_param = 0x00008001; | |
1515 | else if ((asus->driver->brightness > bd->props.brightness) || | |
1516 | bd->props.brightness == 0) | |
1517 | ctrl_param = 0x00008000; | |
3d7b1655 | 1518 | |
c87992d1 | 1519 | asus->driver->brightness = bd->props.brightness; |
3d7b1655 | 1520 | |
c87992d1 AK |
1521 | return ctrl_param; |
1522 | } | |
1523 | ||
3d7b1655 YW |
1524 | static int update_bl_status(struct backlight_device *bd) |
1525 | { | |
1d070f89 | 1526 | struct asus_wmi *asus = bl_get_data(bd); |
dfed65d5 | 1527 | u32 ctrl_param; |
6e0044be | 1528 | int power, err = 0; |
b7187265 | 1529 | |
1d070f89 | 1530 | power = read_backlight_power(asus); |
b7187265 CC |
1531 | if (power != -ENODEV && bd->props.power != power) { |
1532 | ctrl_param = !!(bd->props.power == FB_BLANK_UNBLANK); | |
d33da3b6 CC |
1533 | err = asus_wmi_set_devstate(ASUS_WMI_DEVID_BACKLIGHT, |
1534 | ctrl_param, NULL); | |
6e0044be AK |
1535 | if (asus->driver->quirks->store_backlight_power) |
1536 | asus->driver->panel_power = bd->props.power; | |
6e0044be | 1537 | |
ade28abd CC |
1538 | /* When using scalar brightness, updating the brightness |
1539 | * will mess with the backlight power */ | |
1540 | if (asus->driver->quirks->scalar_panel_brightness) | |
1541 | return err; | |
b7187265 | 1542 | } |
ade28abd CC |
1543 | |
1544 | if (asus->driver->quirks->scalar_panel_brightness) | |
1545 | ctrl_param = get_scalar_command(bd); | |
1546 | else | |
1547 | ctrl_param = bd->props.brightness; | |
1548 | ||
1549 | err = asus_wmi_set_devstate(ASUS_WMI_DEVID_BRIGHTNESS, | |
1550 | ctrl_param, NULL); | |
1551 | ||
8fbea019 | 1552 | return err; |
3d7b1655 YW |
1553 | } |
1554 | ||
e12e6d94 | 1555 | static const struct backlight_ops asus_wmi_bl_ops = { |
3d7b1655 YW |
1556 | .get_brightness = read_brightness, |
1557 | .update_status = update_bl_status, | |
1558 | }; | |
1559 | ||
e12e6d94 | 1560 | static int asus_wmi_backlight_notify(struct asus_wmi *asus, int code) |
3d7b1655 | 1561 | { |
e12e6d94 | 1562 | struct backlight_device *bd = asus->backlight_device; |
3d7b1655 | 1563 | int old = bd->props.brightness; |
b7670ed6 | 1564 | int new = old; |
3d7b1655 YW |
1565 | |
1566 | if (code >= NOTIFY_BRNUP_MIN && code <= NOTIFY_BRNUP_MAX) | |
1567 | new = code - NOTIFY_BRNUP_MIN + 1; | |
1568 | else if (code >= NOTIFY_BRNDOWN_MIN && code <= NOTIFY_BRNDOWN_MAX) | |
1569 | new = code - NOTIFY_BRNDOWN_MIN; | |
1570 | ||
1571 | bd->props.brightness = new; | |
1572 | backlight_update_status(bd); | |
1573 | backlight_force_update(bd, BACKLIGHT_UPDATE_HOTKEY); | |
1574 | ||
1575 | return old; | |
1576 | } | |
1577 | ||
e12e6d94 | 1578 | static int asus_wmi_backlight_init(struct asus_wmi *asus) |
3d7b1655 YW |
1579 | { |
1580 | struct backlight_device *bd; | |
1581 | struct backlight_properties props; | |
b7187265 CC |
1582 | int max; |
1583 | int power; | |
1584 | ||
8fbea019 | 1585 | max = read_brightness_max(asus); |
86ac2735 | 1586 | if (max < 0) |
8fbea019 CC |
1587 | return max; |
1588 | ||
1589 | power = read_backlight_power(asus); | |
1590 | ||
b7187265 CC |
1591 | if (power == -ENODEV) |
1592 | power = FB_BLANK_UNBLANK; | |
8fbea019 CC |
1593 | else if (power < 0) |
1594 | return power; | |
3d7b1655 YW |
1595 | |
1596 | memset(&props, 0, sizeof(struct backlight_properties)); | |
60cfa098 | 1597 | props.type = BACKLIGHT_PLATFORM; |
b7187265 | 1598 | props.max_brightness = max; |
e12e6d94 CC |
1599 | bd = backlight_device_register(asus->driver->name, |
1600 | &asus->platform_device->dev, asus, | |
1601 | &asus_wmi_bl_ops, &props); | |
3d7b1655 YW |
1602 | if (IS_ERR(bd)) { |
1603 | pr_err("Could not register backlight device\n"); | |
1604 | return PTR_ERR(bd); | |
1605 | } | |
1606 | ||
e12e6d94 | 1607 | asus->backlight_device = bd; |
3d7b1655 | 1608 | |
6e0044be AK |
1609 | if (asus->driver->quirks->store_backlight_power) |
1610 | asus->driver->panel_power = power; | |
1611 | ||
3d7b1655 | 1612 | bd->props.brightness = read_brightness(bd); |
b7187265 | 1613 | bd->props.power = power; |
3d7b1655 YW |
1614 | backlight_update_status(bd); |
1615 | ||
c87992d1 AK |
1616 | asus->driver->brightness = bd->props.brightness; |
1617 | ||
3d7b1655 YW |
1618 | return 0; |
1619 | } | |
1620 | ||
e12e6d94 | 1621 | static void asus_wmi_backlight_exit(struct asus_wmi *asus) |
3d7b1655 | 1622 | { |
00981810 | 1623 | backlight_device_unregister(asus->backlight_device); |
3d7b1655 | 1624 | |
e12e6d94 | 1625 | asus->backlight_device = NULL; |
3d7b1655 YW |
1626 | } |
1627 | ||
a2a96f0c AK |
1628 | static int is_display_toggle(int code) |
1629 | { | |
1630 | /* display toggle keys */ | |
1631 | if ((code >= 0x61 && code <= 0x67) || | |
1632 | (code >= 0x8c && code <= 0x93) || | |
1633 | (code >= 0xa0 && code <= 0xa7) || | |
1634 | (code >= 0xd0 && code <= 0xd5)) | |
1635 | return 1; | |
1636 | ||
1637 | return 0; | |
1638 | } | |
1639 | ||
e12e6d94 | 1640 | static void asus_wmi_notify(u32 value, void *context) |
3d7b1655 | 1641 | { |
e12e6d94 | 1642 | struct asus_wmi *asus = context; |
3d7b1655 YW |
1643 | struct acpi_buffer response = { ACPI_ALLOCATE_BUFFER, NULL }; |
1644 | union acpi_object *obj; | |
1645 | acpi_status status; | |
1646 | int code; | |
1647 | int orig_code; | |
c4453f6a SF |
1648 | unsigned int key_value = 1; |
1649 | bool autorelease = 1; | |
3d7b1655 YW |
1650 | |
1651 | status = wmi_get_event_data(value, &response); | |
1652 | if (status != AE_OK) { | |
1653 | pr_err("bad event status 0x%x\n", status); | |
1654 | return; | |
1655 | } | |
1656 | ||
1657 | obj = (union acpi_object *)response.pointer; | |
1658 | ||
57ab7dae CC |
1659 | if (!obj || obj->type != ACPI_TYPE_INTEGER) |
1660 | goto exit; | |
3d7b1655 | 1661 | |
57ab7dae CC |
1662 | code = obj->integer.value; |
1663 | orig_code = code; | |
3d7b1655 | 1664 | |
c4453f6a SF |
1665 | if (asus->driver->key_filter) { |
1666 | asus->driver->key_filter(asus->driver, &code, &key_value, | |
1667 | &autorelease); | |
1668 | if (code == ASUS_WMI_KEY_IGNORE) | |
1669 | goto exit; | |
1670 | } | |
1671 | ||
57ab7dae | 1672 | if (code >= NOTIFY_BRNUP_MIN && code <= NOTIFY_BRNUP_MAX) |
3ba0302b | 1673 | code = ASUS_WMI_BRN_UP; |
57ab7dae CC |
1674 | else if (code >= NOTIFY_BRNDOWN_MIN && |
1675 | code <= NOTIFY_BRNDOWN_MAX) | |
3ba0302b | 1676 | code = ASUS_WMI_BRN_DOWN; |
3d7b1655 | 1677 | |
3ba0302b | 1678 | if (code == ASUS_WMI_BRN_DOWN || code == ASUS_WMI_BRN_UP) { |
62c4aa1a | 1679 | if (acpi_video_get_backlight_type() == acpi_backlight_vendor) { |
57ab7dae | 1680 | asus_wmi_backlight_notify(asus, orig_code); |
3ba0302b | 1681 | goto exit; |
a2a96f0c | 1682 | } |
a2a96f0c AK |
1683 | } |
1684 | ||
1685 | if (is_display_toggle(code) && | |
1686 | asus->driver->quirks->no_display_toggle) | |
1687 | goto exit; | |
1688 | ||
1689 | if (!sparse_keymap_report_event(asus->inputdev, code, | |
1690 | key_value, autorelease)) | |
57ab7dae | 1691 | pr_info("Unknown key %x pressed\n", code); |
3d7b1655 | 1692 | |
57ab7dae | 1693 | exit: |
3d7b1655 YW |
1694 | kfree(obj); |
1695 | } | |
1696 | ||
9e1565bc CC |
1697 | /* |
1698 | * Sys helpers | |
1699 | */ | |
1700 | static int parse_arg(const char *buf, unsigned long count, int *val) | |
1701 | { | |
1702 | if (!count) | |
1703 | return 0; | |
1704 | if (sscanf(buf, "%i", val) != 1) | |
1705 | return -EINVAL; | |
1706 | return count; | |
1707 | } | |
1708 | ||
1d070f89 CC |
1709 | static ssize_t store_sys_wmi(struct asus_wmi *asus, int devid, |
1710 | const char *buf, size_t count) | |
9e1565bc | 1711 | { |
9e1565bc | 1712 | u32 retval; |
d33da3b6 | 1713 | int rv, err, value; |
9e1565bc | 1714 | |
1d070f89 | 1715 | value = asus_wmi_get_devstate_simple(asus, devid); |
b8298340 | 1716 | if (value < 0) |
9e1565bc CC |
1717 | return value; |
1718 | ||
1719 | rv = parse_arg(buf, count, &value); | |
d33da3b6 CC |
1720 | err = asus_wmi_set_devstate(devid, value, &retval); |
1721 | ||
1722 | if (err < 0) | |
1723 | return err; | |
9e1565bc | 1724 | |
9e1565bc CC |
1725 | return rv; |
1726 | } | |
1727 | ||
1d070f89 | 1728 | static ssize_t show_sys_wmi(struct asus_wmi *asus, int devid, char *buf) |
9e1565bc | 1729 | { |
1d070f89 | 1730 | int value = asus_wmi_get_devstate_simple(asus, devid); |
9e1565bc CC |
1731 | |
1732 | if (value < 0) | |
1733 | return value; | |
1734 | ||
1735 | return sprintf(buf, "%d\n", value); | |
1736 | } | |
1737 | ||
e12e6d94 | 1738 | #define ASUS_WMI_CREATE_DEVICE_ATTR(_name, _mode, _cm) \ |
9e1565bc CC |
1739 | static ssize_t show_##_name(struct device *dev, \ |
1740 | struct device_attribute *attr, \ | |
1741 | char *buf) \ | |
1742 | { \ | |
1d070f89 CC |
1743 | struct asus_wmi *asus = dev_get_drvdata(dev); \ |
1744 | \ | |
1745 | return show_sys_wmi(asus, _cm, buf); \ | |
9e1565bc CC |
1746 | } \ |
1747 | static ssize_t store_##_name(struct device *dev, \ | |
1748 | struct device_attribute *attr, \ | |
1749 | const char *buf, size_t count) \ | |
1750 | { \ | |
1d070f89 CC |
1751 | struct asus_wmi *asus = dev_get_drvdata(dev); \ |
1752 | \ | |
1753 | return store_sys_wmi(asus, _cm, buf, count); \ | |
9e1565bc CC |
1754 | } \ |
1755 | static struct device_attribute dev_attr_##_name = { \ | |
1756 | .attr = { \ | |
1757 | .name = __stringify(_name), \ | |
1758 | .mode = _mode }, \ | |
1759 | .show = show_##_name, \ | |
1760 | .store = store_##_name, \ | |
1761 | } | |
1762 | ||
e12e6d94 CC |
1763 | ASUS_WMI_CREATE_DEVICE_ATTR(touchpad, 0644, ASUS_WMI_DEVID_TOUCHPAD); |
1764 | ASUS_WMI_CREATE_DEVICE_ATTR(camera, 0644, ASUS_WMI_DEVID_CAMERA); | |
1765 | ASUS_WMI_CREATE_DEVICE_ATTR(cardr, 0644, ASUS_WMI_DEVID_CARDREADER); | |
c0b91b6d | 1766 | ASUS_WMI_CREATE_DEVICE_ATTR(lid_resume, 0644, ASUS_WMI_DEVID_LID_RESUME); |
aca234f6 | 1767 | ASUS_WMI_CREATE_DEVICE_ATTR(als_enable, 0644, ASUS_WMI_DEVID_ALS_ENABLE); |
9e1565bc | 1768 | |
a8fe3428 | 1769 | static ssize_t cpufv_store(struct device *dev, struct device_attribute *attr, |
67fa38ec | 1770 | const char *buf, size_t count) |
7f80d734 | 1771 | { |
3df5fdad | 1772 | int value, rv; |
7f80d734 CB |
1773 | |
1774 | if (!count || sscanf(buf, "%i", &value) != 1) | |
1775 | return -EINVAL; | |
1776 | if (value < 0 || value > 2) | |
1777 | return -EINVAL; | |
1778 | ||
3df5fdad CC |
1779 | rv = asus_wmi_evaluate_method(ASUS_WMI_METHODID_CFVS, value, 0, NULL); |
1780 | if (rv < 0) | |
1781 | return rv; | |
1782 | ||
1783 | return count; | |
7f80d734 CB |
1784 | } |
1785 | ||
a8fe3428 | 1786 | static DEVICE_ATTR_WO(cpufv); |
7f80d734 | 1787 | |
4e37b42d CC |
1788 | static struct attribute *platform_attributes[] = { |
1789 | &dev_attr_cpufv.attr, | |
9e1565bc CC |
1790 | &dev_attr_camera.attr, |
1791 | &dev_attr_cardr.attr, | |
4615bb66 | 1792 | &dev_attr_touchpad.attr, |
c0b91b6d | 1793 | &dev_attr_lid_resume.attr, |
aca234f6 | 1794 | &dev_attr_als_enable.attr, |
4e37b42d CC |
1795 | NULL |
1796 | }; | |
1797 | ||
587a1f16 | 1798 | static umode_t asus_sysfs_is_visible(struct kobject *kobj, |
e12e6d94 | 1799 | struct attribute *attr, int idx) |
9e1565bc | 1800 | { |
1d070f89 CC |
1801 | struct device *dev = container_of(kobj, struct device, kobj); |
1802 | struct platform_device *pdev = to_platform_device(dev); | |
1803 | struct asus_wmi *asus = platform_get_drvdata(pdev); | |
1804 | bool ok = true; | |
9e1565bc CC |
1805 | int devid = -1; |
1806 | ||
1807 | if (attr == &dev_attr_camera.attr) | |
e12e6d94 | 1808 | devid = ASUS_WMI_DEVID_CAMERA; |
9e1565bc | 1809 | else if (attr == &dev_attr_cardr.attr) |
e12e6d94 | 1810 | devid = ASUS_WMI_DEVID_CARDREADER; |
4615bb66 | 1811 | else if (attr == &dev_attr_touchpad.attr) |
e12e6d94 | 1812 | devid = ASUS_WMI_DEVID_TOUCHPAD; |
c0b91b6d AK |
1813 | else if (attr == &dev_attr_lid_resume.attr) |
1814 | devid = ASUS_WMI_DEVID_LID_RESUME; | |
aca234f6 OR |
1815 | else if (attr == &dev_attr_als_enable.attr) |
1816 | devid = ASUS_WMI_DEVID_ALS_ENABLE; | |
9e1565bc CC |
1817 | |
1818 | if (devid != -1) | |
1d070f89 | 1819 | ok = !(asus_wmi_get_devstate_simple(asus, devid) < 0); |
9e1565bc | 1820 | |
1d070f89 | 1821 | return ok ? attr->mode : 0; |
9e1565bc CC |
1822 | } |
1823 | ||
e90d9ba8 | 1824 | static const struct attribute_group platform_attribute_group = { |
e12e6d94 CC |
1825 | .is_visible = asus_sysfs_is_visible, |
1826 | .attrs = platform_attributes | |
4e37b42d CC |
1827 | }; |
1828 | ||
e12e6d94 | 1829 | static void asus_wmi_sysfs_exit(struct platform_device *device) |
7f80d734 | 1830 | { |
4e37b42d | 1831 | sysfs_remove_group(&device->dev.kobj, &platform_attribute_group); |
7f80d734 CB |
1832 | } |
1833 | ||
e12e6d94 | 1834 | static int asus_wmi_sysfs_init(struct platform_device *device) |
7f80d734 | 1835 | { |
4e37b42d | 1836 | return sysfs_create_group(&device->dev.kobj, &platform_attribute_group); |
7f80d734 CB |
1837 | } |
1838 | ||
27c136c8 CC |
1839 | /* |
1840 | * Platform device | |
1841 | */ | |
39ddf3bf | 1842 | static int asus_wmi_platform_init(struct asus_wmi *asus) |
ee027e4a | 1843 | { |
46dbca87 CC |
1844 | int rv; |
1845 | ||
1846 | /* INIT enable hotkeys on some models */ | |
1847 | if (!asus_wmi_evaluate_method(ASUS_WMI_METHODID_INIT, 0, 0, &rv)) | |
0ed60654 | 1848 | pr_info("Initialization: %#x\n", rv); |
46dbca87 CC |
1849 | |
1850 | /* We don't know yet what to do with this version... */ | |
1851 | if (!asus_wmi_evaluate_method(ASUS_WMI_METHODID_SPEC, 0, 0x9, &rv)) { | |
0ed60654 | 1852 | pr_info("BIOS WMI version: %d.%d\n", rv >> 16, rv & 0xFF); |
46dbca87 CC |
1853 | asus->spec = rv; |
1854 | } | |
1855 | ||
1856 | /* | |
1857 | * The SFUN method probably allows the original driver to get the list | |
1858 | * of features supported by a given model. For now, 0x0100 or 0x0800 | |
1859 | * bit signifies that the laptop is equipped with a Wi-Fi MiniPCI card. | |
1860 | * The significance of others is yet to be found. | |
1861 | */ | |
1862 | if (!asus_wmi_evaluate_method(ASUS_WMI_METHODID_SFUN, 0, 0, &rv)) { | |
0ed60654 | 1863 | pr_info("SFUN value: %#x\n", rv); |
46dbca87 CC |
1864 | asus->sfun = rv; |
1865 | } | |
1866 | ||
1d070f89 CC |
1867 | /* |
1868 | * Eee PC and Notebooks seems to have different method_id for DSTS, | |
1869 | * but it may also be related to the BIOS's SPEC. | |
1870 | * Note, on most Eeepc, there is no way to check if a method exist | |
1871 | * or note, while on notebooks, they returns 0xFFFFFFFE on failure, | |
1872 | * but once again, SPEC may probably be used for that kind of things. | |
1873 | */ | |
1874 | if (!asus_wmi_evaluate_method(ASUS_WMI_METHODID_DSTS, 0, 0, NULL)) | |
1875 | asus->dsts_id = ASUS_WMI_METHODID_DSTS; | |
63a78bb1 | 1876 | else |
1d070f89 CC |
1877 | asus->dsts_id = ASUS_WMI_METHODID_DSTS2; |
1878 | ||
fddbfed5 CC |
1879 | /* CWAP allow to define the behavior of the Fn+F2 key, |
1880 | * this method doesn't seems to be present on Eee PCs */ | |
6a2bcccd | 1881 | if (asus->driver->quirks->wapf >= 0) |
fddbfed5 | 1882 | asus_wmi_set_devstate(ASUS_WMI_DEVID_CWAP, |
6a2bcccd | 1883 | asus->driver->quirks->wapf, NULL); |
fddbfed5 | 1884 | |
e12e6d94 | 1885 | return asus_wmi_sysfs_init(asus->platform_device); |
27c136c8 CC |
1886 | } |
1887 | ||
e12e6d94 | 1888 | static void asus_wmi_platform_exit(struct asus_wmi *asus) |
27c136c8 | 1889 | { |
e12e6d94 | 1890 | asus_wmi_sysfs_exit(asus->platform_device); |
27c136c8 CC |
1891 | } |
1892 | ||
8c1b2d83 CC |
1893 | /* |
1894 | * debugfs | |
1895 | */ | |
e12e6d94 CC |
1896 | struct asus_wmi_debugfs_node { |
1897 | struct asus_wmi *asus; | |
8c1b2d83 | 1898 | char *name; |
e12e6d94 | 1899 | int (*show) (struct seq_file *m, void *data); |
8c1b2d83 CC |
1900 | }; |
1901 | ||
1902 | static int show_dsts(struct seq_file *m, void *data) | |
1903 | { | |
e12e6d94 | 1904 | struct asus_wmi *asus = m->private; |
d33da3b6 | 1905 | int err; |
8c1b2d83 CC |
1906 | u32 retval = -1; |
1907 | ||
1d070f89 | 1908 | err = asus_wmi_get_devstate(asus, asus->debug.dev_id, &retval); |
8c1b2d83 | 1909 | |
d33da3b6 CC |
1910 | if (err < 0) |
1911 | return err; | |
8c1b2d83 | 1912 | |
ef343491 | 1913 | seq_printf(m, "DSTS(%#x) = %#x\n", asus->debug.dev_id, retval); |
8c1b2d83 CC |
1914 | |
1915 | return 0; | |
1916 | } | |
1917 | ||
1918 | static int show_devs(struct seq_file *m, void *data) | |
1919 | { | |
e12e6d94 | 1920 | struct asus_wmi *asus = m->private; |
d33da3b6 | 1921 | int err; |
8c1b2d83 CC |
1922 | u32 retval = -1; |
1923 | ||
d33da3b6 CC |
1924 | err = asus_wmi_set_devstate(asus->debug.dev_id, asus->debug.ctrl_param, |
1925 | &retval); | |
1926 | ||
1927 | if (err < 0) | |
1928 | return err; | |
8c1b2d83 | 1929 | |
ef343491 | 1930 | seq_printf(m, "DEVS(%#x, %#x) = %#x\n", asus->debug.dev_id, |
e12e6d94 | 1931 | asus->debug.ctrl_param, retval); |
8c1b2d83 CC |
1932 | |
1933 | return 0; | |
1934 | } | |
1935 | ||
ef343491 CC |
1936 | static int show_call(struct seq_file *m, void *data) |
1937 | { | |
1938 | struct asus_wmi *asus = m->private; | |
1939 | struct bios_args args = { | |
1940 | .arg0 = asus->debug.dev_id, | |
1941 | .arg1 = asus->debug.ctrl_param, | |
1942 | }; | |
1943 | struct acpi_buffer input = { (acpi_size) sizeof(args), &args }; | |
1944 | struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER, NULL }; | |
1945 | union acpi_object *obj; | |
1946 | acpi_status status; | |
1947 | ||
1948 | status = wmi_evaluate_method(ASUS_WMI_MGMT_GUID, | |
1949 | 1, asus->debug.method_id, | |
1950 | &input, &output); | |
1951 | ||
1952 | if (ACPI_FAILURE(status)) | |
1953 | return -EIO; | |
1954 | ||
1955 | obj = (union acpi_object *)output.pointer; | |
1956 | if (obj && obj->type == ACPI_TYPE_INTEGER) | |
1957 | seq_printf(m, "%#x(%#x, %#x) = %#x\n", asus->debug.method_id, | |
1958 | asus->debug.dev_id, asus->debug.ctrl_param, | |
1959 | (u32) obj->integer.value); | |
1960 | else | |
1961 | seq_printf(m, "%#x(%#x, %#x) = t:%d\n", asus->debug.method_id, | |
1962 | asus->debug.dev_id, asus->debug.ctrl_param, | |
a1d60867 | 1963 | obj ? obj->type : -1); |
ef343491 CC |
1964 | |
1965 | kfree(obj); | |
1966 | ||
1967 | return 0; | |
1968 | } | |
1969 | ||
e12e6d94 CC |
1970 | static struct asus_wmi_debugfs_node asus_wmi_debug_files[] = { |
1971 | {NULL, "devs", show_devs}, | |
1972 | {NULL, "dsts", show_dsts}, | |
ef343491 | 1973 | {NULL, "call", show_call}, |
8c1b2d83 CC |
1974 | }; |
1975 | ||
e12e6d94 | 1976 | static int asus_wmi_debugfs_open(struct inode *inode, struct file *file) |
8c1b2d83 | 1977 | { |
e12e6d94 | 1978 | struct asus_wmi_debugfs_node *node = inode->i_private; |
8c1b2d83 | 1979 | |
e12e6d94 | 1980 | return single_open(file, node->show, node->asus); |
8c1b2d83 CC |
1981 | } |
1982 | ||
e12e6d94 | 1983 | static const struct file_operations asus_wmi_debugfs_io_ops = { |
8c1b2d83 | 1984 | .owner = THIS_MODULE, |
e12e6d94 | 1985 | .open = asus_wmi_debugfs_open, |
8c1b2d83 CC |
1986 | .read = seq_read, |
1987 | .llseek = seq_lseek, | |
1988 | .release = single_release, | |
1989 | }; | |
1990 | ||
e12e6d94 | 1991 | static void asus_wmi_debugfs_exit(struct asus_wmi *asus) |
8c1b2d83 | 1992 | { |
e12e6d94 | 1993 | debugfs_remove_recursive(asus->debug.root); |
8c1b2d83 CC |
1994 | } |
1995 | ||
e12e6d94 | 1996 | static int asus_wmi_debugfs_init(struct asus_wmi *asus) |
8c1b2d83 CC |
1997 | { |
1998 | struct dentry *dent; | |
1999 | int i; | |
2000 | ||
e12e6d94 CC |
2001 | asus->debug.root = debugfs_create_dir(asus->driver->name, NULL); |
2002 | if (!asus->debug.root) { | |
0ed60654 | 2003 | pr_err("failed to create debugfs directory\n"); |
8c1b2d83 CC |
2004 | goto error_debugfs; |
2005 | } | |
2006 | ||
ef343491 CC |
2007 | dent = debugfs_create_x32("method_id", S_IRUGO | S_IWUSR, |
2008 | asus->debug.root, &asus->debug.method_id); | |
2009 | if (!dent) | |
2010 | goto error_debugfs; | |
2011 | ||
e12e6d94 CC |
2012 | dent = debugfs_create_x32("dev_id", S_IRUGO | S_IWUSR, |
2013 | asus->debug.root, &asus->debug.dev_id); | |
8c1b2d83 CC |
2014 | if (!dent) |
2015 | goto error_debugfs; | |
2016 | ||
e12e6d94 CC |
2017 | dent = debugfs_create_x32("ctrl_param", S_IRUGO | S_IWUSR, |
2018 | asus->debug.root, &asus->debug.ctrl_param); | |
8c1b2d83 CC |
2019 | if (!dent) |
2020 | goto error_debugfs; | |
2021 | ||
e12e6d94 CC |
2022 | for (i = 0; i < ARRAY_SIZE(asus_wmi_debug_files); i++) { |
2023 | struct asus_wmi_debugfs_node *node = &asus_wmi_debug_files[i]; | |
8c1b2d83 | 2024 | |
e12e6d94 | 2025 | node->asus = asus; |
8c1b2d83 | 2026 | dent = debugfs_create_file(node->name, S_IFREG | S_IRUGO, |
e12e6d94 CC |
2027 | asus->debug.root, node, |
2028 | &asus_wmi_debugfs_io_ops); | |
8c1b2d83 CC |
2029 | if (!dent) { |
2030 | pr_err("failed to create debug file: %s\n", node->name); | |
2031 | goto error_debugfs; | |
2032 | } | |
2033 | } | |
2034 | ||
2035 | return 0; | |
2036 | ||
2037 | error_debugfs: | |
e12e6d94 | 2038 | asus_wmi_debugfs_exit(asus); |
8c1b2d83 CC |
2039 | return -ENOMEM; |
2040 | } | |
2041 | ||
53e755c2 KB |
2042 | static int asus_wmi_fan_init(struct asus_wmi *asus) |
2043 | { | |
2044 | int status; | |
2045 | ||
2046 | asus->asus_hwmon_pwm = -1; | |
2047 | asus->asus_hwmon_num_fans = -1; | |
2048 | asus->asus_hwmon_fan_manual_mode = false; | |
2049 | ||
2050 | status = asus_hwmon_get_fan_number(asus, &asus->asus_hwmon_num_fans); | |
2051 | if (status) { | |
2052 | asus->asus_hwmon_num_fans = 0; | |
2053 | pr_warn("Could not determine number of fans: %d\n", status); | |
2054 | return -ENXIO; | |
2055 | } | |
2056 | ||
2057 | pr_info("Number of fans: %d\n", asus->asus_hwmon_num_fans); | |
2058 | return 0; | |
2059 | } | |
2060 | ||
71050ae7 JPRV |
2061 | static bool ashs_present(void) |
2062 | { | |
2063 | int i = 0; | |
2064 | while (ashs_ids[i]) { | |
2065 | if (acpi_dev_found(ashs_ids[i++])) | |
2066 | return true; | |
2067 | } | |
2068 | return false; | |
2069 | } | |
2070 | ||
27c136c8 CC |
2071 | /* |
2072 | * WMI Driver | |
2073 | */ | |
e12e6d94 | 2074 | static int asus_wmi_add(struct platform_device *pdev) |
afa7c886 | 2075 | { |
e12e6d94 CC |
2076 | struct platform_driver *pdrv = to_platform_driver(pdev->dev.driver); |
2077 | struct asus_wmi_driver *wdrv = to_asus_wmi_driver(pdrv); | |
2078 | struct asus_wmi *asus; | |
d8c66f62 | 2079 | const char *chassis_type; |
ee027e4a | 2080 | acpi_status status; |
27c136c8 | 2081 | int err; |
a50bd128 | 2082 | u32 result; |
ee027e4a | 2083 | |
e12e6d94 CC |
2084 | asus = kzalloc(sizeof(struct asus_wmi), GFP_KERNEL); |
2085 | if (!asus) | |
a04ce290 CC |
2086 | return -ENOMEM; |
2087 | ||
e12e6d94 CC |
2088 | asus->driver = wdrv; |
2089 | asus->platform_device = pdev; | |
2090 | wdrv->platform_device = pdev; | |
2091 | platform_set_drvdata(asus->platform_device, asus); | |
27c136c8 | 2092 | |
c87992d1 AK |
2093 | if (wdrv->detect_quirks) |
2094 | wdrv->detect_quirks(asus->driver); | |
afa7c886 | 2095 | |
e12e6d94 | 2096 | err = asus_wmi_platform_init(asus); |
27c136c8 CC |
2097 | if (err) |
2098 | goto fail_platform; | |
45f2c693 | 2099 | |
e12e6d94 | 2100 | err = asus_wmi_input_init(asus); |
45f2c693 | 2101 | if (err) |
27c136c8 | 2102 | goto fail_input; |
3d7b1655 | 2103 | |
53e755c2 KB |
2104 | err = asus_wmi_fan_init(asus); /* probably no problems on error */ |
2105 | asus_hwmon_fan_set_auto(asus); | |
2106 | ||
e07babde CC |
2107 | err = asus_wmi_hwmon_init(asus); |
2108 | if (err) | |
2109 | goto fail_hwmon; | |
2110 | ||
e12e6d94 | 2111 | err = asus_wmi_led_init(asus); |
084fca63 CC |
2112 | if (err) |
2113 | goto fail_leds; | |
2114 | ||
71050ae7 JPRV |
2115 | asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_WLAN, &result); |
2116 | if (result & (ASUS_WMI_DSTS_PRESENCE_BIT | ASUS_WMI_DSTS_USER_BIT)) | |
2117 | asus->driver->wlan_ctrl_by_user = 1; | |
2118 | ||
d1c4e9bf | 2119 | if (!(asus->driver->wlan_ctrl_by_user && ashs_present())) { |
a977e59c JPRV |
2120 | err = asus_wmi_rfkill_init(asus); |
2121 | if (err) | |
2122 | goto fail_rfkill; | |
2123 | } | |
ba48fdb9 | 2124 | |
e9b61518 OR |
2125 | if (asus->driver->quirks->wmi_force_als_set) |
2126 | asus_wmi_set_als(); | |
2127 | ||
d8c66f62 HG |
2128 | /* Some Asus desktop boards export an acpi-video backlight interface, |
2129 | stop this from showing up */ | |
2130 | chassis_type = dmi_get_system_info(DMI_CHASSIS_TYPE); | |
2131 | if (chassis_type && !strcmp(chassis_type, "3")) | |
62c4aa1a HG |
2132 | acpi_video_set_dmi_backlight_type(acpi_backlight_vendor); |
2133 | ||
272c77d5 | 2134 | if (asus->driver->quirks->wmi_backlight_power) |
62c4aa1a HG |
2135 | acpi_video_set_dmi_backlight_type(acpi_backlight_vendor); |
2136 | ||
999d4376 | 2137 | if (asus->driver->quirks->wmi_backlight_native) |
2138 | acpi_video_set_dmi_backlight_type(acpi_backlight_native); | |
2139 | ||
8023eff1 KCH |
2140 | if (asus->driver->quirks->xusb2pr) |
2141 | asus_wmi_set_xusb2pr(asus); | |
2142 | ||
62c4aa1a | 2143 | if (acpi_video_get_backlight_type() == acpi_backlight_vendor) { |
e12e6d94 | 2144 | err = asus_wmi_backlight_init(asus); |
b7187265 | 2145 | if (err && err != -ENODEV) |
27c136c8 | 2146 | goto fail_backlight; |
62c4aa1a | 2147 | } |
45f2c693 | 2148 | |
e12e6d94 CC |
2149 | status = wmi_install_notify_handler(asus->driver->event_guid, |
2150 | asus_wmi_notify, asus); | |
45f2c693 | 2151 | if (ACPI_FAILURE(status)) { |
e12e6d94 | 2152 | pr_err("Unable to register notify handler - %d\n", status); |
45f2c693 | 2153 | err = -ENODEV; |
27c136c8 | 2154 | goto fail_wmi_handler; |
45f2c693 YW |
2155 | } |
2156 | ||
e12e6d94 | 2157 | err = asus_wmi_debugfs_init(asus); |
8c1b2d83 CC |
2158 | if (err) |
2159 | goto fail_debugfs; | |
2160 | ||
a04ce290 | 2161 | return 0; |
45f2c693 | 2162 | |
8c1b2d83 | 2163 | fail_debugfs: |
e12e6d94 | 2164 | wmi_remove_notify_handler(asus->driver->event_guid); |
27c136c8 | 2165 | fail_wmi_handler: |
e12e6d94 | 2166 | asus_wmi_backlight_exit(asus); |
27c136c8 | 2167 | fail_backlight: |
e12e6d94 | 2168 | asus_wmi_rfkill_exit(asus); |
ba48fdb9 | 2169 | fail_rfkill: |
e12e6d94 | 2170 | asus_wmi_led_exit(asus); |
084fca63 | 2171 | fail_leds: |
e07babde | 2172 | fail_hwmon: |
e12e6d94 | 2173 | asus_wmi_input_exit(asus); |
27c136c8 | 2174 | fail_input: |
e12e6d94 | 2175 | asus_wmi_platform_exit(asus); |
27c136c8 | 2176 | fail_platform: |
e12e6d94 | 2177 | kfree(asus); |
a04ce290 | 2178 | return err; |
45f2c693 YW |
2179 | } |
2180 | ||
e12e6d94 | 2181 | static int asus_wmi_remove(struct platform_device *device) |
45f2c693 | 2182 | { |
e12e6d94 CC |
2183 | struct asus_wmi *asus; |
2184 | ||
2185 | asus = platform_get_drvdata(device); | |
2186 | wmi_remove_notify_handler(asus->driver->event_guid); | |
2187 | asus_wmi_backlight_exit(asus); | |
2188 | asus_wmi_input_exit(asus); | |
2189 | asus_wmi_led_exit(asus); | |
2190 | asus_wmi_rfkill_exit(asus); | |
2191 | asus_wmi_debugfs_exit(asus); | |
2192 | asus_wmi_platform_exit(asus); | |
53e755c2 | 2193 | asus_hwmon_fan_set_auto(asus); |
e12e6d94 CC |
2194 | |
2195 | kfree(asus); | |
45f2c693 YW |
2196 | return 0; |
2197 | } | |
2198 | ||
0773d7f9 CC |
2199 | /* |
2200 | * Platform driver - hibernate/resume callbacks | |
2201 | */ | |
e12e6d94 | 2202 | static int asus_hotk_thaw(struct device *device) |
0773d7f9 | 2203 | { |
e12e6d94 | 2204 | struct asus_wmi *asus = dev_get_drvdata(device); |
0773d7f9 | 2205 | |
a7ce3f04 | 2206 | if (asus->wlan.rfkill) { |
0773d7f9 CC |
2207 | bool wlan; |
2208 | ||
2209 | /* | |
2210 | * Work around bios bug - acpi _PTS turns off the wireless led | |
2211 | * during suspend. Normally it restores it on resume, but | |
2212 | * we should kick it ourselves in case hibernation is aborted. | |
2213 | */ | |
1d070f89 | 2214 | wlan = asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_WLAN); |
e12e6d94 | 2215 | asus_wmi_set_devstate(ASUS_WMI_DEVID_WLAN, wlan, NULL); |
0773d7f9 CC |
2216 | } |
2217 | ||
2218 | return 0; | |
2219 | } | |
2220 | ||
30734049 OR |
2221 | static int asus_hotk_resume(struct device *device) |
2222 | { | |
2223 | struct asus_wmi *asus = dev_get_drvdata(device); | |
2224 | ||
2225 | if (!IS_ERR_OR_NULL(asus->kbd_led.dev)) | |
2226 | queue_work(asus->led_workqueue, &asus->kbd_led_work); | |
2227 | ||
2228 | return 0; | |
2229 | } | |
2230 | ||
e12e6d94 | 2231 | static int asus_hotk_restore(struct device *device) |
0773d7f9 | 2232 | { |
e12e6d94 | 2233 | struct asus_wmi *asus = dev_get_drvdata(device); |
0773d7f9 CC |
2234 | int bl; |
2235 | ||
2236 | /* Refresh both wlan rfkill state and pci hotplug */ | |
a7ce3f04 | 2237 | if (asus->wlan.rfkill) |
e12e6d94 | 2238 | asus_rfkill_hotplug(asus); |
0773d7f9 | 2239 | |
a7ce3f04 | 2240 | if (asus->bluetooth.rfkill) { |
1d070f89 CC |
2241 | bl = !asus_wmi_get_devstate_simple(asus, |
2242 | ASUS_WMI_DEVID_BLUETOOTH); | |
a7ce3f04 | 2243 | rfkill_set_sw_state(asus->bluetooth.rfkill, bl); |
2e9e159d | 2244 | } |
a7ce3f04 | 2245 | if (asus->wimax.rfkill) { |
1d070f89 | 2246 | bl = !asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_WIMAX); |
a7ce3f04 | 2247 | rfkill_set_sw_state(asus->wimax.rfkill, bl); |
2e9e159d | 2248 | } |
a7ce3f04 | 2249 | if (asus->wwan3g.rfkill) { |
1d070f89 | 2250 | bl = !asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_WWAN3G); |
a7ce3f04 | 2251 | rfkill_set_sw_state(asus->wwan3g.rfkill, bl); |
0773d7f9 | 2252 | } |
43be8bde CC |
2253 | if (asus->gps.rfkill) { |
2254 | bl = !asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_GPS); | |
2255 | rfkill_set_sw_state(asus->gps.rfkill, bl); | |
2256 | } | |
a912d329 CC |
2257 | if (asus->uwb.rfkill) { |
2258 | bl = !asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_UWB); | |
2259 | rfkill_set_sw_state(asus->uwb.rfkill, bl); | |
2260 | } | |
30734049 OR |
2261 | if (!IS_ERR_OR_NULL(asus->kbd_led.dev)) |
2262 | queue_work(asus->led_workqueue, &asus->kbd_led_work); | |
0773d7f9 CC |
2263 | |
2264 | return 0; | |
2265 | } | |
2266 | ||
e12e6d94 CC |
2267 | static const struct dev_pm_ops asus_pm_ops = { |
2268 | .thaw = asus_hotk_thaw, | |
2269 | .restore = asus_hotk_restore, | |
30734049 | 2270 | .resume = asus_hotk_resume, |
45f2c693 YW |
2271 | }; |
2272 | ||
e12e6d94 | 2273 | static int asus_wmi_probe(struct platform_device *pdev) |
d358cb55 | 2274 | { |
e12e6d94 CC |
2275 | struct platform_driver *pdrv = to_platform_driver(pdev->dev.driver); |
2276 | struct asus_wmi_driver *wdrv = to_asus_wmi_driver(pdrv); | |
2277 | int ret; | |
d358cb55 | 2278 | |
e12e6d94 | 2279 | if (!wmi_has_guid(ASUS_WMI_MGMT_GUID)) { |
5ad77dcf | 2280 | pr_warn("Management GUID not found\n"); |
ee027e4a YW |
2281 | return -ENODEV; |
2282 | } | |
2283 | ||
e12e6d94 | 2284 | if (wdrv->event_guid && !wmi_has_guid(wdrv->event_guid)) { |
5ad77dcf | 2285 | pr_warn("Event GUID not found\n"); |
d358cb55 CC |
2286 | return -ENODEV; |
2287 | } | |
2288 | ||
e12e6d94 CC |
2289 | if (wdrv->probe) { |
2290 | ret = wdrv->probe(pdev); | |
2291 | if (ret) | |
2292 | return ret; | |
2293 | } | |
2294 | ||
2295 | return asus_wmi_add(pdev); | |
a04ce290 | 2296 | } |
45f2c693 | 2297 | |
e12e6d94 | 2298 | static bool used; |
ee027e4a | 2299 | |
8fe8c25e | 2300 | int __init_or_module asus_wmi_register_driver(struct asus_wmi_driver *driver) |
a04ce290 | 2301 | { |
e12e6d94 CC |
2302 | struct platform_driver *platform_driver; |
2303 | struct platform_device *platform_device; | |
2304 | ||
2305 | if (used) | |
2306 | return -EBUSY; | |
2307 | ||
2308 | platform_driver = &driver->platform_driver; | |
2309 | platform_driver->remove = asus_wmi_remove; | |
2310 | platform_driver->driver.owner = driver->owner; | |
2311 | platform_driver->driver.name = driver->name; | |
2312 | platform_driver->driver.pm = &asus_pm_ops; | |
2313 | ||
2314 | platform_device = platform_create_bundle(platform_driver, | |
2315 | asus_wmi_probe, | |
a04ce290 CC |
2316 | NULL, 0, NULL, 0); |
2317 | if (IS_ERR(platform_device)) | |
2318 | return PTR_ERR(platform_device); | |
e12e6d94 CC |
2319 | |
2320 | used = true; | |
2321 | return 0; | |
2322 | } | |
2323 | EXPORT_SYMBOL_GPL(asus_wmi_register_driver); | |
2324 | ||
2325 | void asus_wmi_unregister_driver(struct asus_wmi_driver *driver) | |
2326 | { | |
2327 | platform_device_unregister(driver->platform_device); | |
2328 | platform_driver_unregister(&driver->platform_driver); | |
2329 | used = false; | |
2330 | } | |
2331 | EXPORT_SYMBOL_GPL(asus_wmi_unregister_driver); | |
2332 | ||
2333 | static int __init asus_wmi_init(void) | |
2334 | { | |
2335 | if (!wmi_has_guid(ASUS_WMI_MGMT_GUID)) { | |
0ed60654 | 2336 | pr_info("Asus Management GUID not found\n"); |
e12e6d94 CC |
2337 | return -ENODEV; |
2338 | } | |
2339 | ||
0ed60654 | 2340 | pr_info("ASUS WMI generic driver loaded\n"); |
ee027e4a YW |
2341 | return 0; |
2342 | } | |
2343 | ||
e12e6d94 | 2344 | static void __exit asus_wmi_exit(void) |
ee027e4a | 2345 | { |
0ed60654 | 2346 | pr_info("ASUS WMI generic driver unloaded\n"); |
ee027e4a YW |
2347 | } |
2348 | ||
e12e6d94 CC |
2349 | module_init(asus_wmi_init); |
2350 | module_exit(asus_wmi_exit); |