]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - drivers/platform/x86/asus-wmi.c
Merge tag 'for-linus-4.14c-rc6-tag' of git://git.kernel.org/pub/scm/linux/kernel...
[mirror_ubuntu-bionic-kernel.git] / drivers / platform / x86 / asus-wmi.c
1 /*
2 * Asus PC WMI hotkey driver
3 *
4 * Copyright(C) 2010 Intel Corporation.
5 * Copyright(C) 2010-2011 Corentin Chary <corentin.chary@gmail.com>
6 *
7 * Portions based on wistron_btns.c:
8 * Copyright (C) 2005 Miloslav Trmac <mitr@volny.cz>
9 * Copyright (C) 2005 Bernhard Rosenkraenzer <bero@arklinux.org>
10 * Copyright (C) 2005 Dmitry Torokhov <dtor@mail.ru>
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 */
26
27 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
28
29 #include <linux/kernel.h>
30 #include <linux/module.h>
31 #include <linux/init.h>
32 #include <linux/types.h>
33 #include <linux/slab.h>
34 #include <linux/input.h>
35 #include <linux/input/sparse-keymap.h>
36 #include <linux/fb.h>
37 #include <linux/backlight.h>
38 #include <linux/leds.h>
39 #include <linux/rfkill.h>
40 #include <linux/pci.h>
41 #include <linux/pci_hotplug.h>
42 #include <linux/hwmon.h>
43 #include <linux/hwmon-sysfs.h>
44 #include <linux/debugfs.h>
45 #include <linux/seq_file.h>
46 #include <linux/platform_device.h>
47 #include <linux/thermal.h>
48 #include <linux/acpi.h>
49 #include <linux/dmi.h>
50 #include <acpi/video.h>
51
52 #include "asus-wmi.h"
53
54 MODULE_AUTHOR("Corentin Chary <corentin.chary@gmail.com>, "
55 "Yong Wang <yong.y.wang@intel.com>");
56 MODULE_DESCRIPTION("Asus Generic WMI Driver");
57 MODULE_LICENSE("GPL");
58
59 #define to_asus_wmi_driver(pdrv) \
60 (container_of((pdrv), struct asus_wmi_driver, platform_driver))
61
62 #define ASUS_WMI_MGMT_GUID "97845ED0-4E6D-11DE-8A39-0800200C9A66"
63
64 #define NOTIFY_BRNUP_MIN 0x11
65 #define NOTIFY_BRNUP_MAX 0x1f
66 #define NOTIFY_BRNDOWN_MIN 0x20
67 #define NOTIFY_BRNDOWN_MAX 0x2e
68 #define NOTIFY_KBD_BRTUP 0xc4
69 #define NOTIFY_KBD_BRTDWN 0xc5
70
71 /* WMI Methods */
72 #define ASUS_WMI_METHODID_SPEC 0x43455053 /* BIOS SPECification */
73 #define ASUS_WMI_METHODID_SFBD 0x44424653 /* Set First Boot Device */
74 #define ASUS_WMI_METHODID_GLCD 0x44434C47 /* Get LCD status */
75 #define ASUS_WMI_METHODID_GPID 0x44495047 /* Get Panel ID?? (Resol) */
76 #define ASUS_WMI_METHODID_QMOD 0x444F4D51 /* Quiet MODe */
77 #define ASUS_WMI_METHODID_SPLV 0x4C425053 /* Set Panel Light Value */
78 #define ASUS_WMI_METHODID_AGFN 0x4E464741 /* FaN? */
79 #define ASUS_WMI_METHODID_SFUN 0x4E554653 /* FUNCtionalities */
80 #define ASUS_WMI_METHODID_SDSP 0x50534453 /* Set DiSPlay output */
81 #define ASUS_WMI_METHODID_GDSP 0x50534447 /* Get DiSPlay output */
82 #define ASUS_WMI_METHODID_DEVP 0x50564544 /* DEVice Policy */
83 #define ASUS_WMI_METHODID_OSVR 0x5256534F /* OS VeRsion */
84 #define ASUS_WMI_METHODID_DSTS 0x53544344 /* Device STatuS */
85 #define ASUS_WMI_METHODID_DSTS2 0x53545344 /* Device STatuS #2*/
86 #define ASUS_WMI_METHODID_BSTS 0x53545342 /* Bios STatuS ? */
87 #define ASUS_WMI_METHODID_DEVS 0x53564544 /* DEVice Set */
88 #define ASUS_WMI_METHODID_CFVS 0x53564643 /* CPU Frequency Volt Set */
89 #define ASUS_WMI_METHODID_KBFT 0x5446424B /* KeyBoard FilTer */
90 #define ASUS_WMI_METHODID_INIT 0x54494E49 /* INITialize */
91 #define ASUS_WMI_METHODID_HKEY 0x59454B48 /* Hot KEY ?? */
92
93 #define ASUS_WMI_UNSUPPORTED_METHOD 0xFFFFFFFE
94
95 /* Wireless */
96 #define ASUS_WMI_DEVID_HW_SWITCH 0x00010001
97 #define ASUS_WMI_DEVID_WIRELESS_LED 0x00010002
98 #define ASUS_WMI_DEVID_CWAP 0x00010003
99 #define ASUS_WMI_DEVID_WLAN 0x00010011
100 #define ASUS_WMI_DEVID_WLAN_LED 0x00010012
101 #define ASUS_WMI_DEVID_BLUETOOTH 0x00010013
102 #define ASUS_WMI_DEVID_GPS 0x00010015
103 #define ASUS_WMI_DEVID_WIMAX 0x00010017
104 #define ASUS_WMI_DEVID_WWAN3G 0x00010019
105 #define ASUS_WMI_DEVID_UWB 0x00010021
106
107 /* Leds */
108 /* 0x000200XX and 0x000400XX */
109 #define ASUS_WMI_DEVID_LED1 0x00020011
110 #define ASUS_WMI_DEVID_LED2 0x00020012
111 #define ASUS_WMI_DEVID_LED3 0x00020013
112 #define ASUS_WMI_DEVID_LED4 0x00020014
113 #define ASUS_WMI_DEVID_LED5 0x00020015
114 #define ASUS_WMI_DEVID_LED6 0x00020016
115
116 /* Backlight and Brightness */
117 #define ASUS_WMI_DEVID_ALS_ENABLE 0x00050001 /* Ambient Light Sensor */
118 #define ASUS_WMI_DEVID_BACKLIGHT 0x00050011
119 #define ASUS_WMI_DEVID_BRIGHTNESS 0x00050012
120 #define ASUS_WMI_DEVID_KBD_BACKLIGHT 0x00050021
121 #define ASUS_WMI_DEVID_LIGHT_SENSOR 0x00050022 /* ?? */
122
123 /* Misc */
124 #define ASUS_WMI_DEVID_CAMERA 0x00060013
125
126 /* Storage */
127 #define ASUS_WMI_DEVID_CARDREADER 0x00080013
128
129 /* Input */
130 #define ASUS_WMI_DEVID_TOUCHPAD 0x00100011
131 #define ASUS_WMI_DEVID_TOUCHPAD_LED 0x00100012
132
133 /* Fan, Thermal */
134 #define ASUS_WMI_DEVID_THERMAL_CTRL 0x00110011
135 #define ASUS_WMI_DEVID_FAN_CTRL 0x00110012
136
137 /* Power */
138 #define ASUS_WMI_DEVID_PROCESSOR_STATE 0x00120012
139
140 /* Deep S3 / Resume on LID open */
141 #define ASUS_WMI_DEVID_LID_RESUME 0x00120031
142
143 /* DSTS masks */
144 #define ASUS_WMI_DSTS_STATUS_BIT 0x00000001
145 #define ASUS_WMI_DSTS_UNKNOWN_BIT 0x00000002
146 #define ASUS_WMI_DSTS_PRESENCE_BIT 0x00010000
147 #define ASUS_WMI_DSTS_USER_BIT 0x00020000
148 #define ASUS_WMI_DSTS_BIOS_BIT 0x00040000
149 #define ASUS_WMI_DSTS_BRIGHTNESS_MASK 0x000000FF
150 #define ASUS_WMI_DSTS_MAX_BRIGTH_MASK 0x0000FF00
151
152 #define ASUS_FAN_DESC "cpu_fan"
153 #define ASUS_FAN_MFUN 0x13
154 #define ASUS_FAN_SFUN_READ 0x06
155 #define ASUS_FAN_SFUN_WRITE 0x07
156 #define ASUS_FAN_CTRL_MANUAL 1
157 #define ASUS_FAN_CTRL_AUTO 2
158
159 #define USB_INTEL_XUSB2PR 0xD0
160 #define PCI_DEVICE_ID_INTEL_LYNXPOINT_LP_XHCI 0x9c31
161
162 static const char * const ashs_ids[] = { "ATK4001", "ATK4002", NULL };
163
164 struct bios_args {
165 u32 arg0;
166 u32 arg1;
167 } __packed;
168
169 /*
170 * Struct that's used for all methods called via AGFN. Naming is
171 * identically to the AML code.
172 */
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
188 /*
189 * <platform>/ - debugfs root directory
190 * dev_id - current dev_id
191 * ctrl_param - current ctrl_param
192 * method_id - current method_id
193 * devs - call DEVS(dev_id, ctrl_param) and print result
194 * dsts - call DSTS(dev_id) and print result
195 * call - call method_id(dev_id, ctrl_param) and print result
196 */
197 struct asus_wmi_debug {
198 struct dentry *root;
199 u32 method_id;
200 u32 dev_id;
201 u32 ctrl_param;
202 };
203
204 struct asus_rfkill {
205 struct asus_wmi *asus;
206 struct rfkill *rfkill;
207 u32 dev_id;
208 };
209
210 struct asus_wmi {
211 int dsts_id;
212 int spec;
213 int sfun;
214
215 struct input_dev *inputdev;
216 struct backlight_device *backlight_device;
217 struct platform_device *platform_device;
218
219 struct led_classdev wlan_led;
220 int wlan_led_wk;
221 struct led_classdev tpd_led;
222 int tpd_led_wk;
223 struct led_classdev kbd_led;
224 int kbd_led_wk;
225 struct workqueue_struct *led_workqueue;
226 struct work_struct tpd_led_work;
227 struct work_struct kbd_led_work;
228 struct work_struct wlan_led_work;
229
230 struct asus_rfkill wlan;
231 struct asus_rfkill bluetooth;
232 struct asus_rfkill wimax;
233 struct asus_rfkill wwan3g;
234 struct asus_rfkill gps;
235 struct asus_rfkill uwb;
236
237 bool asus_hwmon_fan_manual_mode;
238 int asus_hwmon_num_fans;
239 int asus_hwmon_pwm;
240
241 struct hotplug_slot *hotplug_slot;
242 struct mutex hotplug_lock;
243 struct mutex wmi_lock;
244 struct workqueue_struct *hotplug_workqueue;
245 struct work_struct hotplug_work;
246
247 struct asus_wmi_debug debug;
248
249 struct asus_wmi_driver *driver;
250 };
251
252 static int asus_wmi_input_init(struct asus_wmi *asus)
253 {
254 int err;
255
256 asus->inputdev = input_allocate_device();
257 if (!asus->inputdev)
258 return -ENOMEM;
259
260 asus->inputdev->name = asus->driver->input_name;
261 asus->inputdev->phys = asus->driver->input_phys;
262 asus->inputdev->id.bustype = BUS_HOST;
263 asus->inputdev->dev.parent = &asus->platform_device->dev;
264 set_bit(EV_REP, asus->inputdev->evbit);
265
266 err = sparse_keymap_setup(asus->inputdev, asus->driver->keymap, NULL);
267 if (err)
268 goto err_free_dev;
269
270 err = input_register_device(asus->inputdev);
271 if (err)
272 goto err_free_dev;
273
274 return 0;
275
276 err_free_dev:
277 input_free_device(asus->inputdev);
278 return err;
279 }
280
281 static void asus_wmi_input_exit(struct asus_wmi *asus)
282 {
283 if (asus->inputdev)
284 input_unregister_device(asus->inputdev);
285
286 asus->inputdev = NULL;
287 }
288
289 static int asus_wmi_evaluate_method(u32 method_id, u32 arg0, u32 arg1,
290 u32 *retval)
291 {
292 struct bios_args args = {
293 .arg0 = arg0,
294 .arg1 = arg1,
295 };
296 struct acpi_buffer input = { (acpi_size) sizeof(args), &args };
297 struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER, NULL };
298 acpi_status status;
299 union acpi_object *obj;
300 u32 tmp = 0;
301
302 status = wmi_evaluate_method(ASUS_WMI_MGMT_GUID, 0, method_id,
303 &input, &output);
304
305 if (ACPI_FAILURE(status))
306 goto exit;
307
308 obj = (union acpi_object *)output.pointer;
309 if (obj && obj->type == ACPI_TYPE_INTEGER)
310 tmp = (u32) obj->integer.value;
311
312 if (retval)
313 *retval = tmp;
314
315 kfree(obj);
316
317 exit:
318 if (ACPI_FAILURE(status))
319 return -EIO;
320
321 if (tmp == ASUS_WMI_UNSUPPORTED_METHOD)
322 return -ENODEV;
323
324 return 0;
325 }
326
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
357 static int asus_wmi_get_devstate(struct asus_wmi *asus, u32 dev_id, u32 *retval)
358 {
359 return asus_wmi_evaluate_method(asus->dsts_id, dev_id, 0, retval);
360 }
361
362 static int asus_wmi_set_devstate(u32 dev_id, u32 ctrl_param,
363 u32 *retval)
364 {
365 return asus_wmi_evaluate_method(ASUS_WMI_METHODID_DEVS, dev_id,
366 ctrl_param, retval);
367 }
368
369 /* Helper for special devices with magic return codes */
370 static int asus_wmi_get_devstate_bits(struct asus_wmi *asus,
371 u32 dev_id, u32 mask)
372 {
373 u32 retval = 0;
374 int err;
375
376 err = asus_wmi_get_devstate(asus, dev_id, &retval);
377
378 if (err < 0)
379 return err;
380
381 if (!(retval & ASUS_WMI_DSTS_PRESENCE_BIT))
382 return -ENODEV;
383
384 if (mask == ASUS_WMI_DSTS_STATUS_BIT) {
385 if (retval & ASUS_WMI_DSTS_UNKNOWN_BIT)
386 return -ENODEV;
387 }
388
389 return retval & mask;
390 }
391
392 static int asus_wmi_get_devstate_simple(struct asus_wmi *asus, u32 dev_id)
393 {
394 return asus_wmi_get_devstate_bits(asus, dev_id,
395 ASUS_WMI_DSTS_STATUS_BIT);
396 }
397
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
404 * subsystem asks, we avoid messing with the Asus ACPI stuff during a
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;
410 struct asus_wmi *asus;
411
412 asus = container_of(work, struct asus_wmi, tpd_led_work);
413
414 ctrl_param = asus->tpd_led_wk;
415 asus_wmi_set_devstate(ASUS_WMI_DEVID_TOUCHPAD_LED, ctrl_param, NULL);
416 }
417
418 static void tpd_led_set(struct led_classdev *led_cdev,
419 enum led_brightness value)
420 {
421 struct asus_wmi *asus;
422
423 asus = container_of(led_cdev, struct asus_wmi, tpd_led);
424
425 asus->tpd_led_wk = !!value;
426 queue_work(asus->led_workqueue, &asus->tpd_led_work);
427 }
428
429 static int read_tpd_led_state(struct asus_wmi *asus)
430 {
431 return asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_TOUCHPAD_LED);
432 }
433
434 static enum led_brightness tpd_led_get(struct led_classdev *led_cdev)
435 {
436 struct asus_wmi *asus;
437
438 asus = container_of(led_cdev, struct asus_wmi, tpd_led);
439
440 return read_tpd_led_state(asus);
441 }
442
443 static void kbd_led_update(struct work_struct *work)
444 {
445 int ctrl_param = 0;
446 struct asus_wmi *asus;
447
448 asus = container_of(work, struct asus_wmi, kbd_led_work);
449
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);
456
457 asus_wmi_set_devstate(ASUS_WMI_DEVID_KBD_BACKLIGHT, ctrl_param, NULL);
458 }
459
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
473 /* Unknown status is considered as off */
474 if (retval == 0x8000)
475 retval = 0;
476
477 if (retval >= 0) {
478 if (level)
479 *level = retval & 0x7F;
480 if (env)
481 *env = (retval >> 8) & 0x7F;
482 retval = 0;
483 }
484
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;
517 }
518
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
570 static void asus_wmi_led_exit(struct asus_wmi *asus)
571 {
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))
575 led_classdev_unregister(&asus->tpd_led);
576 if (!IS_ERR_OR_NULL(asus->wlan_led.dev))
577 led_classdev_unregister(&asus->wlan_led);
578 if (asus->led_workqueue)
579 destroy_workqueue(asus->led_workqueue);
580 }
581
582 static int asus_wmi_led_init(struct asus_wmi *asus)
583 {
584 int rv = 0, led_val;
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
604 led_val = kbd_led_read(asus, NULL, NULL);
605 if (led_val >= 0) {
606 INIT_WORK(&asus->kbd_led_work, kbd_led_update);
607
608 asus->kbd_led_wk = led_val;
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);
616 if (rv)
617 goto error;
618 }
619
620 if (wlan_led_presence(asus) && (asus->driver->quirks->wapf > 0)) {
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);
633 }
634
635 error:
636 if (rv)
637 asus_wmi_led_exit(asus);
638
639 return rv;
640 }
641
642
643 /*
644 * PCI hotplug (for wlan rfkill)
645 */
646 static bool asus_wlan_rfkill_blocked(struct asus_wmi *asus)
647 {
648 int result = asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_WLAN);
649
650 if (result < 0)
651 return false;
652 return !result;
653 }
654
655 static void asus_rfkill_hotplug(struct asus_wmi *asus)
656 {
657 struct pci_dev *dev;
658 struct pci_bus *bus;
659 bool blocked;
660 bool absent;
661 u32 l;
662
663 mutex_lock(&asus->wmi_lock);
664 blocked = asus_wlan_rfkill_blocked(asus);
665 mutex_unlock(&asus->wmi_lock);
666
667 mutex_lock(&asus->hotplug_lock);
668 pci_lock_rescan_remove();
669
670 if (asus->wlan.rfkill)
671 rfkill_set_sw_state(asus->wlan.rfkill, blocked);
672
673 if (asus->hotplug_slot) {
674 bus = pci_find_bus(0, 1);
675 if (!bus) {
676 pr_warn("Unable to find PCI bus 1?\n");
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) {
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");
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);
706 pci_bus_add_device(dev);
707 }
708 } else {
709 dev = pci_get_slot(bus, 0);
710 if (dev) {
711 pci_stop_and_remove_bus_device(dev);
712 pci_dev_put(dev);
713 }
714 }
715 }
716
717 out_unlock:
718 pci_unlock_rescan_remove();
719 mutex_unlock(&asus->hotplug_lock);
720 }
721
722 static void asus_rfkill_notify(acpi_handle handle, u32 event, void *data)
723 {
724 struct asus_wmi *asus = data;
725
726 if (event != ACPI_NOTIFY_BUS_CHECK)
727 return;
728
729 /*
730 * We can't call directly asus_rfkill_hotplug because most
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
733 * method to be serialized, we schedule a asus_rfkill_hotplug
734 * call later, in a safer context.
735 */
736 queue_work(asus->hotplug_workqueue, &asus->hotplug_work);
737 }
738
739 static int asus_register_rfkill_notifier(struct asus_wmi *asus, char *node)
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,
749 asus_rfkill_notify, asus);
750 if (ACPI_FAILURE(status))
751 pr_warn("Failed to register notify on %s\n", node);
752 } else
753 return -ENODEV;
754
755 return 0;
756 }
757
758 static void asus_unregister_rfkill_notifier(struct asus_wmi *asus, char *node)
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,
767 ACPI_SYSTEM_NOTIFY,
768 asus_rfkill_notify);
769 if (ACPI_FAILURE(status))
770 pr_err("Error removing rfkill notify handler %s\n",
771 node);
772 }
773 }
774
775 static int asus_get_adapter_status(struct hotplug_slot *hotplug_slot,
776 u8 *value)
777 {
778 struct asus_wmi *asus = hotplug_slot->private;
779 int result = asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_WLAN);
780
781 if (result < 0)
782 return result;
783
784 *value = !!result;
785 return 0;
786 }
787
788 static void asus_cleanup_pci_hotplug(struct hotplug_slot *hotplug_slot)
789 {
790 kfree(hotplug_slot->info);
791 kfree(hotplug_slot);
792 }
793
794 static struct hotplug_slot_ops asus_hotplug_slot_ops = {
795 .owner = THIS_MODULE,
796 .get_adapter_status = asus_get_adapter_status,
797 .get_power_status = asus_get_adapter_status,
798 };
799
800 static void asus_hotplug_work(struct work_struct *work)
801 {
802 struct asus_wmi *asus;
803
804 asus = container_of(work, struct asus_wmi, hotplug_work);
805 asus_rfkill_hotplug(asus);
806 }
807
808 static int asus_setup_pci_hotplug(struct asus_wmi *asus)
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
818 asus->hotplug_workqueue =
819 create_singlethread_workqueue("hotplug_workqueue");
820 if (!asus->hotplug_workqueue)
821 goto error_workqueue;
822
823 INIT_WORK(&asus->hotplug_work, asus_hotplug_work);
824
825 asus->hotplug_slot = kzalloc(sizeof(struct hotplug_slot), GFP_KERNEL);
826 if (!asus->hotplug_slot)
827 goto error_slot;
828
829 asus->hotplug_slot->info = kzalloc(sizeof(struct hotplug_slot_info),
830 GFP_KERNEL);
831 if (!asus->hotplug_slot->info)
832 goto error_info;
833
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);
839
840 ret = pci_hp_register(asus->hotplug_slot, bus, 0, "asus-wifi");
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:
849 kfree(asus->hotplug_slot->info);
850 error_info:
851 kfree(asus->hotplug_slot);
852 asus->hotplug_slot = NULL;
853 error_slot:
854 destroy_workqueue(asus->hotplug_workqueue);
855 error_workqueue:
856 return ret;
857 }
858
859 /*
860 * Rfkill devices
861 */
862 static int asus_rfkill_set(void *data, bool blocked)
863 {
864 struct asus_rfkill *priv = data;
865 u32 ctrl_param = !blocked;
866 u32 dev_id = priv->dev_id;
867
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);
881 }
882
883 static void asus_rfkill_query(struct rfkill *rfkill, void *data)
884 {
885 struct asus_rfkill *priv = data;
886 int result;
887
888 result = asus_wmi_get_devstate_simple(priv->asus, priv->dev_id);
889
890 if (result < 0)
891 return;
892
893 rfkill_set_sw_state(priv->rfkill, !result);
894 }
895
896 static int asus_rfkill_wlan_set(void *data, bool blocked)
897 {
898 struct asus_rfkill *priv = data;
899 struct asus_wmi *asus = priv->asus;
900 int ret;
901
902 /*
903 * This handler is enabled only if hotplug is enabled.
904 * In this case, the asus_wmi_set_devstate() will
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 */
909 mutex_lock(&asus->wmi_lock);
910 ret = asus_rfkill_set(data, blocked);
911 mutex_unlock(&asus->wmi_lock);
912 return ret;
913 }
914
915 static const struct rfkill_ops asus_rfkill_wlan_ops = {
916 .set_block = asus_rfkill_wlan_set,
917 .query = asus_rfkill_query,
918 };
919
920 static const struct rfkill_ops asus_rfkill_ops = {
921 .set_block = asus_rfkill_set,
922 .query = asus_rfkill_query,
923 };
924
925 static int asus_new_rfkill(struct asus_wmi *asus,
926 struct asus_rfkill *arfkill,
927 const char *name, enum rfkill_type type, int dev_id)
928 {
929 int result = asus_wmi_get_devstate_simple(asus, dev_id);
930 struct rfkill **rfkill = &arfkill->rfkill;
931
932 if (result < 0)
933 return result;
934
935 arfkill->dev_id = dev_id;
936 arfkill->asus = asus;
937
938 if (dev_id == ASUS_WMI_DEVID_WLAN &&
939 asus->driver->quirks->hotplug_wireless)
940 *rfkill = rfkill_alloc(name, &asus->platform_device->dev, type,
941 &asus_rfkill_wlan_ops, arfkill);
942 else
943 *rfkill = rfkill_alloc(name, &asus->platform_device->dev, type,
944 &asus_rfkill_ops, arfkill);
945
946 if (!*rfkill)
947 return -EINVAL;
948
949 if ((dev_id == ASUS_WMI_DEVID_WLAN) &&
950 (asus->driver->quirks->wapf > 0))
951 rfkill_set_led_trigger_name(*rfkill, "asus-wlan");
952
953 rfkill_init_sw_state(*rfkill, !result);
954 result = rfkill_register(*rfkill);
955 if (result) {
956 rfkill_destroy(*rfkill);
957 *rfkill = NULL;
958 return result;
959 }
960 return 0;
961 }
962
963 static void asus_wmi_rfkill_exit(struct asus_wmi *asus)
964 {
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");
968 if (asus->wlan.rfkill) {
969 rfkill_unregister(asus->wlan.rfkill);
970 rfkill_destroy(asus->wlan.rfkill);
971 asus->wlan.rfkill = NULL;
972 }
973 /*
974 * Refresh pci hotplug in case the rfkill state was changed after
975 * asus_unregister_rfkill_notifier()
976 */
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
983 if (asus->bluetooth.rfkill) {
984 rfkill_unregister(asus->bluetooth.rfkill);
985 rfkill_destroy(asus->bluetooth.rfkill);
986 asus->bluetooth.rfkill = NULL;
987 }
988 if (asus->wimax.rfkill) {
989 rfkill_unregister(asus->wimax.rfkill);
990 rfkill_destroy(asus->wimax.rfkill);
991 asus->wimax.rfkill = NULL;
992 }
993 if (asus->wwan3g.rfkill) {
994 rfkill_unregister(asus->wwan3g.rfkill);
995 rfkill_destroy(asus->wwan3g.rfkill);
996 asus->wwan3g.rfkill = NULL;
997 }
998 if (asus->gps.rfkill) {
999 rfkill_unregister(asus->gps.rfkill);
1000 rfkill_destroy(asus->gps.rfkill);
1001 asus->gps.rfkill = NULL;
1002 }
1003 if (asus->uwb.rfkill) {
1004 rfkill_unregister(asus->uwb.rfkill);
1005 rfkill_destroy(asus->uwb.rfkill);
1006 asus->uwb.rfkill = NULL;
1007 }
1008 }
1009
1010 static int asus_wmi_rfkill_init(struct asus_wmi *asus)
1011 {
1012 int result = 0;
1013
1014 mutex_init(&asus->hotplug_lock);
1015 mutex_init(&asus->wmi_lock);
1016
1017 result = asus_new_rfkill(asus, &asus->wlan, "asus-wlan",
1018 RFKILL_TYPE_WLAN, ASUS_WMI_DEVID_WLAN);
1019
1020 if (result && result != -ENODEV)
1021 goto exit;
1022
1023 result = asus_new_rfkill(asus, &asus->bluetooth,
1024 "asus-bluetooth", RFKILL_TYPE_BLUETOOTH,
1025 ASUS_WMI_DEVID_BLUETOOTH);
1026
1027 if (result && result != -ENODEV)
1028 goto exit;
1029
1030 result = asus_new_rfkill(asus, &asus->wimax, "asus-wimax",
1031 RFKILL_TYPE_WIMAX, ASUS_WMI_DEVID_WIMAX);
1032
1033 if (result && result != -ENODEV)
1034 goto exit;
1035
1036 result = asus_new_rfkill(asus, &asus->wwan3g, "asus-wwan3g",
1037 RFKILL_TYPE_WWAN, ASUS_WMI_DEVID_WWAN3G);
1038
1039 if (result && result != -ENODEV)
1040 goto exit;
1041
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
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
1054 if (!asus->driver->quirks->hotplug_wireless)
1055 goto exit;
1056
1057 result = asus_setup_pci_hotplug(asus);
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
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");
1068 /*
1069 * Refresh pci hotplug in case the rfkill state was changed during
1070 * setup.
1071 */
1072 asus_rfkill_hotplug(asus);
1073
1074 exit:
1075 if (result && result != -ENODEV)
1076 asus_wmi_rfkill_exit(asus);
1077
1078 if (result == -ENODEV)
1079 result = 0;
1080
1081 return result;
1082 }
1083
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
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
1116 /*
1117 * Hwmon device
1118 */
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)
1206 {
1207 struct asus_wmi *asus = dev_get_drvdata(dev);
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 {
1226 int err;
1227
1228 if (asus->asus_hwmon_pwm >= 0) {
1229 *value = asus->asus_hwmon_pwm;
1230 return;
1231 }
1232
1233 err = asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_FAN_CTRL, value);
1234 if (err < 0)
1235 return;
1236
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;
1248 }
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);
1259
1260 return sprintf(buf, "%d\n", value);
1261 }
1262
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
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
1354 value = DECI_KELVIN_TO_CELSIUS((value & 0xFFFF)) * 1000;
1355
1356 return sprintf(buf, "%d\n", value);
1357 }
1358
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 */
1366 static DEVICE_ATTR(temp1_input, S_IRUGO, asus_hwmon_temp1, NULL);
1367
1368 static struct attribute *hwmon_attributes[] = {
1369 &dev_attr_pwm1.attr,
1370 &dev_attr_pwm1_enable.attr,
1371 &dev_attr_fan1_input.attr,
1372 &dev_attr_fan1_label.attr,
1373
1374 &dev_attr_temp1_input.attr,
1375 NULL
1376 };
1377
1378 static umode_t asus_hwmon_sysfs_is_visible(struct kobject *kobj,
1379 struct attribute *attr, int idx)
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);
1384 int dev_id = -1;
1385 int fan_attr = -1;
1386 u32 value = ASUS_WMI_UNSUPPORTED_METHOD;
1387 bool ok = true;
1388
1389 if (attr == &dev_attr_pwm1.attr)
1390 dev_id = ASUS_WMI_DEVID_FAN_CTRL;
1391 else if (attr == &dev_attr_temp1_input.attr)
1392 dev_id = ASUS_WMI_DEVID_THERMAL_CTRL;
1393
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
1402 if (dev_id != -1) {
1403 int err = asus_wmi_get_devstate(asus, dev_id, &value);
1404
1405 if (err < 0 && fan_attr == -1)
1406 return 0; /* can't return negative here */
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 */
1418 if (value == ASUS_WMI_UNSUPPORTED_METHOD || value & 0xFFF80000
1419 || (!asus->sfun && !(value & ASUS_WMI_DSTS_PRESENCE_BIT)))
1420 ok = false;
1421 else
1422 ok = fan_attr <= asus->asus_hwmon_num_fans;
1423 } else if (dev_id == ASUS_WMI_DEVID_THERMAL_CTRL) {
1424 /* If value is zero, something is clearly wrong */
1425 if (!value)
1426 ok = false;
1427 } else if (fan_attr <= asus->asus_hwmon_num_fans && fan_attr != -1) {
1428 ok = true;
1429 } else {
1430 ok = false;
1431 }
1432
1433 return ok ? attr->mode : 0;
1434 }
1435
1436 static const struct attribute_group hwmon_attribute_group = {
1437 .is_visible = asus_hwmon_sysfs_is_visible,
1438 .attrs = hwmon_attributes
1439 };
1440 __ATTRIBUTE_GROUPS(hwmon_attribute);
1441
1442 static int asus_wmi_hwmon_init(struct asus_wmi *asus)
1443 {
1444 struct device *hwmon;
1445
1446 hwmon = hwmon_device_register_with_groups(&asus->platform_device->dev,
1447 "asus", asus,
1448 hwmon_attribute_groups);
1449 if (IS_ERR(hwmon)) {
1450 pr_err("Could not register asus hwmon device\n");
1451 return PTR_ERR(hwmon);
1452 }
1453 return 0;
1454 }
1455
1456 /*
1457 * Backlight
1458 */
1459 static int read_backlight_power(struct asus_wmi *asus)
1460 {
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);
1467
1468 if (ret < 0)
1469 return ret;
1470
1471 return ret ? FB_BLANK_UNBLANK : FB_BLANK_POWERDOWN;
1472 }
1473
1474 static int read_brightness_max(struct asus_wmi *asus)
1475 {
1476 u32 retval;
1477 int err;
1478
1479 err = asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_BRIGHTNESS, &retval);
1480
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);
1496 u32 retval;
1497 int err;
1498
1499 err = asus_wmi_get_devstate(asus, ASUS_WMI_DEVID_BRIGHTNESS, &retval);
1500
1501 if (err < 0)
1502 return err;
1503
1504 return retval & ASUS_WMI_DSTS_BRIGHTNESS_MASK;
1505 }
1506
1507 static u32 get_scalar_command(struct backlight_device *bd)
1508 {
1509 struct asus_wmi *asus = bl_get_data(bd);
1510 u32 ctrl_param = 0;
1511
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;
1518
1519 asus->driver->brightness = bd->props.brightness;
1520
1521 return ctrl_param;
1522 }
1523
1524 static int update_bl_status(struct backlight_device *bd)
1525 {
1526 struct asus_wmi *asus = bl_get_data(bd);
1527 u32 ctrl_param;
1528 int power, err = 0;
1529
1530 power = read_backlight_power(asus);
1531 if (power != -ENODEV && bd->props.power != power) {
1532 ctrl_param = !!(bd->props.power == FB_BLANK_UNBLANK);
1533 err = asus_wmi_set_devstate(ASUS_WMI_DEVID_BACKLIGHT,
1534 ctrl_param, NULL);
1535 if (asus->driver->quirks->store_backlight_power)
1536 asus->driver->panel_power = bd->props.power;
1537
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;
1542 }
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
1552 return err;
1553 }
1554
1555 static const struct backlight_ops asus_wmi_bl_ops = {
1556 .get_brightness = read_brightness,
1557 .update_status = update_bl_status,
1558 };
1559
1560 static int asus_wmi_backlight_notify(struct asus_wmi *asus, int code)
1561 {
1562 struct backlight_device *bd = asus->backlight_device;
1563 int old = bd->props.brightness;
1564 int new = old;
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
1578 static int asus_wmi_backlight_init(struct asus_wmi *asus)
1579 {
1580 struct backlight_device *bd;
1581 struct backlight_properties props;
1582 int max;
1583 int power;
1584
1585 max = read_brightness_max(asus);
1586 if (max < 0)
1587 return max;
1588
1589 power = read_backlight_power(asus);
1590
1591 if (power == -ENODEV)
1592 power = FB_BLANK_UNBLANK;
1593 else if (power < 0)
1594 return power;
1595
1596 memset(&props, 0, sizeof(struct backlight_properties));
1597 props.type = BACKLIGHT_PLATFORM;
1598 props.max_brightness = max;
1599 bd = backlight_device_register(asus->driver->name,
1600 &asus->platform_device->dev, asus,
1601 &asus_wmi_bl_ops, &props);
1602 if (IS_ERR(bd)) {
1603 pr_err("Could not register backlight device\n");
1604 return PTR_ERR(bd);
1605 }
1606
1607 asus->backlight_device = bd;
1608
1609 if (asus->driver->quirks->store_backlight_power)
1610 asus->driver->panel_power = power;
1611
1612 bd->props.brightness = read_brightness(bd);
1613 bd->props.power = power;
1614 backlight_update_status(bd);
1615
1616 asus->driver->brightness = bd->props.brightness;
1617
1618 return 0;
1619 }
1620
1621 static void asus_wmi_backlight_exit(struct asus_wmi *asus)
1622 {
1623 backlight_device_unregister(asus->backlight_device);
1624
1625 asus->backlight_device = NULL;
1626 }
1627
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
1640 static void asus_wmi_notify(u32 value, void *context)
1641 {
1642 struct asus_wmi *asus = context;
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;
1648 unsigned int key_value = 1;
1649 bool autorelease = 1;
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
1659 if (!obj || obj->type != ACPI_TYPE_INTEGER)
1660 goto exit;
1661
1662 code = obj->integer.value;
1663 orig_code = code;
1664
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
1672 if (code >= NOTIFY_BRNUP_MIN && code <= NOTIFY_BRNUP_MAX)
1673 code = ASUS_WMI_BRN_UP;
1674 else if (code >= NOTIFY_BRNDOWN_MIN &&
1675 code <= NOTIFY_BRNDOWN_MAX)
1676 code = ASUS_WMI_BRN_DOWN;
1677
1678 if (code == ASUS_WMI_BRN_DOWN || code == ASUS_WMI_BRN_UP) {
1679 if (acpi_video_get_backlight_type() == acpi_backlight_vendor) {
1680 asus_wmi_backlight_notify(asus, orig_code);
1681 goto exit;
1682 }
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))
1691 pr_info("Unknown key %x pressed\n", code);
1692
1693 exit:
1694 kfree(obj);
1695 }
1696
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
1709 static ssize_t store_sys_wmi(struct asus_wmi *asus, int devid,
1710 const char *buf, size_t count)
1711 {
1712 u32 retval;
1713 int rv, err, value;
1714
1715 value = asus_wmi_get_devstate_simple(asus, devid);
1716 if (value < 0)
1717 return value;
1718
1719 rv = parse_arg(buf, count, &value);
1720 err = asus_wmi_set_devstate(devid, value, &retval);
1721
1722 if (err < 0)
1723 return err;
1724
1725 return rv;
1726 }
1727
1728 static ssize_t show_sys_wmi(struct asus_wmi *asus, int devid, char *buf)
1729 {
1730 int value = asus_wmi_get_devstate_simple(asus, devid);
1731
1732 if (value < 0)
1733 return value;
1734
1735 return sprintf(buf, "%d\n", value);
1736 }
1737
1738 #define ASUS_WMI_CREATE_DEVICE_ATTR(_name, _mode, _cm) \
1739 static ssize_t show_##_name(struct device *dev, \
1740 struct device_attribute *attr, \
1741 char *buf) \
1742 { \
1743 struct asus_wmi *asus = dev_get_drvdata(dev); \
1744 \
1745 return show_sys_wmi(asus, _cm, buf); \
1746 } \
1747 static ssize_t store_##_name(struct device *dev, \
1748 struct device_attribute *attr, \
1749 const char *buf, size_t count) \
1750 { \
1751 struct asus_wmi *asus = dev_get_drvdata(dev); \
1752 \
1753 return store_sys_wmi(asus, _cm, buf, count); \
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
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);
1766 ASUS_WMI_CREATE_DEVICE_ATTR(lid_resume, 0644, ASUS_WMI_DEVID_LID_RESUME);
1767 ASUS_WMI_CREATE_DEVICE_ATTR(als_enable, 0644, ASUS_WMI_DEVID_ALS_ENABLE);
1768
1769 static ssize_t cpufv_store(struct device *dev, struct device_attribute *attr,
1770 const char *buf, size_t count)
1771 {
1772 int value, rv;
1773
1774 if (!count || sscanf(buf, "%i", &value) != 1)
1775 return -EINVAL;
1776 if (value < 0 || value > 2)
1777 return -EINVAL;
1778
1779 rv = asus_wmi_evaluate_method(ASUS_WMI_METHODID_CFVS, value, 0, NULL);
1780 if (rv < 0)
1781 return rv;
1782
1783 return count;
1784 }
1785
1786 static DEVICE_ATTR_WO(cpufv);
1787
1788 static struct attribute *platform_attributes[] = {
1789 &dev_attr_cpufv.attr,
1790 &dev_attr_camera.attr,
1791 &dev_attr_cardr.attr,
1792 &dev_attr_touchpad.attr,
1793 &dev_attr_lid_resume.attr,
1794 &dev_attr_als_enable.attr,
1795 NULL
1796 };
1797
1798 static umode_t asus_sysfs_is_visible(struct kobject *kobj,
1799 struct attribute *attr, int idx)
1800 {
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;
1805 int devid = -1;
1806
1807 if (attr == &dev_attr_camera.attr)
1808 devid = ASUS_WMI_DEVID_CAMERA;
1809 else if (attr == &dev_attr_cardr.attr)
1810 devid = ASUS_WMI_DEVID_CARDREADER;
1811 else if (attr == &dev_attr_touchpad.attr)
1812 devid = ASUS_WMI_DEVID_TOUCHPAD;
1813 else if (attr == &dev_attr_lid_resume.attr)
1814 devid = ASUS_WMI_DEVID_LID_RESUME;
1815 else if (attr == &dev_attr_als_enable.attr)
1816 devid = ASUS_WMI_DEVID_ALS_ENABLE;
1817
1818 if (devid != -1)
1819 ok = !(asus_wmi_get_devstate_simple(asus, devid) < 0);
1820
1821 return ok ? attr->mode : 0;
1822 }
1823
1824 static const struct attribute_group platform_attribute_group = {
1825 .is_visible = asus_sysfs_is_visible,
1826 .attrs = platform_attributes
1827 };
1828
1829 static void asus_wmi_sysfs_exit(struct platform_device *device)
1830 {
1831 sysfs_remove_group(&device->dev.kobj, &platform_attribute_group);
1832 }
1833
1834 static int asus_wmi_sysfs_init(struct platform_device *device)
1835 {
1836 return sysfs_create_group(&device->dev.kobj, &platform_attribute_group);
1837 }
1838
1839 /*
1840 * Platform device
1841 */
1842 static int asus_wmi_platform_init(struct asus_wmi *asus)
1843 {
1844 int rv;
1845
1846 /* INIT enable hotkeys on some models */
1847 if (!asus_wmi_evaluate_method(ASUS_WMI_METHODID_INIT, 0, 0, &rv))
1848 pr_info("Initialization: %#x\n", rv);
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)) {
1852 pr_info("BIOS WMI version: %d.%d\n", rv >> 16, rv & 0xFF);
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)) {
1863 pr_info("SFUN value: %#x\n", rv);
1864 asus->sfun = rv;
1865 }
1866
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;
1876 else
1877 asus->dsts_id = ASUS_WMI_METHODID_DSTS2;
1878
1879 /* CWAP allow to define the behavior of the Fn+F2 key,
1880 * this method doesn't seems to be present on Eee PCs */
1881 if (asus->driver->quirks->wapf >= 0)
1882 asus_wmi_set_devstate(ASUS_WMI_DEVID_CWAP,
1883 asus->driver->quirks->wapf, NULL);
1884
1885 return asus_wmi_sysfs_init(asus->platform_device);
1886 }
1887
1888 static void asus_wmi_platform_exit(struct asus_wmi *asus)
1889 {
1890 asus_wmi_sysfs_exit(asus->platform_device);
1891 }
1892
1893 /*
1894 * debugfs
1895 */
1896 struct asus_wmi_debugfs_node {
1897 struct asus_wmi *asus;
1898 char *name;
1899 int (*show) (struct seq_file *m, void *data);
1900 };
1901
1902 static int show_dsts(struct seq_file *m, void *data)
1903 {
1904 struct asus_wmi *asus = m->private;
1905 int err;
1906 u32 retval = -1;
1907
1908 err = asus_wmi_get_devstate(asus, asus->debug.dev_id, &retval);
1909
1910 if (err < 0)
1911 return err;
1912
1913 seq_printf(m, "DSTS(%#x) = %#x\n", asus->debug.dev_id, retval);
1914
1915 return 0;
1916 }
1917
1918 static int show_devs(struct seq_file *m, void *data)
1919 {
1920 struct asus_wmi *asus = m->private;
1921 int err;
1922 u32 retval = -1;
1923
1924 err = asus_wmi_set_devstate(asus->debug.dev_id, asus->debug.ctrl_param,
1925 &retval);
1926
1927 if (err < 0)
1928 return err;
1929
1930 seq_printf(m, "DEVS(%#x, %#x) = %#x\n", asus->debug.dev_id,
1931 asus->debug.ctrl_param, retval);
1932
1933 return 0;
1934 }
1935
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 0, 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,
1963 obj ? obj->type : -1);
1964
1965 kfree(obj);
1966
1967 return 0;
1968 }
1969
1970 static struct asus_wmi_debugfs_node asus_wmi_debug_files[] = {
1971 {NULL, "devs", show_devs},
1972 {NULL, "dsts", show_dsts},
1973 {NULL, "call", show_call},
1974 };
1975
1976 static int asus_wmi_debugfs_open(struct inode *inode, struct file *file)
1977 {
1978 struct asus_wmi_debugfs_node *node = inode->i_private;
1979
1980 return single_open(file, node->show, node->asus);
1981 }
1982
1983 static const struct file_operations asus_wmi_debugfs_io_ops = {
1984 .owner = THIS_MODULE,
1985 .open = asus_wmi_debugfs_open,
1986 .read = seq_read,
1987 .llseek = seq_lseek,
1988 .release = single_release,
1989 };
1990
1991 static void asus_wmi_debugfs_exit(struct asus_wmi *asus)
1992 {
1993 debugfs_remove_recursive(asus->debug.root);
1994 }
1995
1996 static int asus_wmi_debugfs_init(struct asus_wmi *asus)
1997 {
1998 struct dentry *dent;
1999 int i;
2000
2001 asus->debug.root = debugfs_create_dir(asus->driver->name, NULL);
2002 if (!asus->debug.root) {
2003 pr_err("failed to create debugfs directory\n");
2004 goto error_debugfs;
2005 }
2006
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
2012 dent = debugfs_create_x32("dev_id", S_IRUGO | S_IWUSR,
2013 asus->debug.root, &asus->debug.dev_id);
2014 if (!dent)
2015 goto error_debugfs;
2016
2017 dent = debugfs_create_x32("ctrl_param", S_IRUGO | S_IWUSR,
2018 asus->debug.root, &asus->debug.ctrl_param);
2019 if (!dent)
2020 goto error_debugfs;
2021
2022 for (i = 0; i < ARRAY_SIZE(asus_wmi_debug_files); i++) {
2023 struct asus_wmi_debugfs_node *node = &asus_wmi_debug_files[i];
2024
2025 node->asus = asus;
2026 dent = debugfs_create_file(node->name, S_IFREG | S_IRUGO,
2027 asus->debug.root, node,
2028 &asus_wmi_debugfs_io_ops);
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:
2038 asus_wmi_debugfs_exit(asus);
2039 return -ENOMEM;
2040 }
2041
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
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
2071 /*
2072 * WMI Driver
2073 */
2074 static int asus_wmi_add(struct platform_device *pdev)
2075 {
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;
2079 const char *chassis_type;
2080 acpi_status status;
2081 int err;
2082 u32 result;
2083
2084 asus = kzalloc(sizeof(struct asus_wmi), GFP_KERNEL);
2085 if (!asus)
2086 return -ENOMEM;
2087
2088 asus->driver = wdrv;
2089 asus->platform_device = pdev;
2090 wdrv->platform_device = pdev;
2091 platform_set_drvdata(asus->platform_device, asus);
2092
2093 if (wdrv->detect_quirks)
2094 wdrv->detect_quirks(asus->driver);
2095
2096 err = asus_wmi_platform_init(asus);
2097 if (err)
2098 goto fail_platform;
2099
2100 err = asus_wmi_input_init(asus);
2101 if (err)
2102 goto fail_input;
2103
2104 err = asus_wmi_fan_init(asus); /* probably no problems on error */
2105 asus_hwmon_fan_set_auto(asus);
2106
2107 err = asus_wmi_hwmon_init(asus);
2108 if (err)
2109 goto fail_hwmon;
2110
2111 err = asus_wmi_led_init(asus);
2112 if (err)
2113 goto fail_leds;
2114
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
2119 if (!(asus->driver->wlan_ctrl_by_user && ashs_present())) {
2120 err = asus_wmi_rfkill_init(asus);
2121 if (err)
2122 goto fail_rfkill;
2123 }
2124
2125 if (asus->driver->quirks->wmi_force_als_set)
2126 asus_wmi_set_als();
2127
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"))
2132 acpi_video_set_dmi_backlight_type(acpi_backlight_vendor);
2133
2134 if (asus->driver->quirks->wmi_backlight_power)
2135 acpi_video_set_dmi_backlight_type(acpi_backlight_vendor);
2136
2137 if (asus->driver->quirks->wmi_backlight_native)
2138 acpi_video_set_dmi_backlight_type(acpi_backlight_native);
2139
2140 if (asus->driver->quirks->xusb2pr)
2141 asus_wmi_set_xusb2pr(asus);
2142
2143 if (acpi_video_get_backlight_type() == acpi_backlight_vendor) {
2144 err = asus_wmi_backlight_init(asus);
2145 if (err && err != -ENODEV)
2146 goto fail_backlight;
2147 }
2148
2149 status = wmi_install_notify_handler(asus->driver->event_guid,
2150 asus_wmi_notify, asus);
2151 if (ACPI_FAILURE(status)) {
2152 pr_err("Unable to register notify handler - %d\n", status);
2153 err = -ENODEV;
2154 goto fail_wmi_handler;
2155 }
2156
2157 err = asus_wmi_debugfs_init(asus);
2158 if (err)
2159 goto fail_debugfs;
2160
2161 return 0;
2162
2163 fail_debugfs:
2164 wmi_remove_notify_handler(asus->driver->event_guid);
2165 fail_wmi_handler:
2166 asus_wmi_backlight_exit(asus);
2167 fail_backlight:
2168 asus_wmi_rfkill_exit(asus);
2169 fail_rfkill:
2170 asus_wmi_led_exit(asus);
2171 fail_leds:
2172 fail_hwmon:
2173 asus_wmi_input_exit(asus);
2174 fail_input:
2175 asus_wmi_platform_exit(asus);
2176 fail_platform:
2177 kfree(asus);
2178 return err;
2179 }
2180
2181 static int asus_wmi_remove(struct platform_device *device)
2182 {
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);
2193 asus_hwmon_fan_set_auto(asus);
2194
2195 kfree(asus);
2196 return 0;
2197 }
2198
2199 /*
2200 * Platform driver - hibernate/resume callbacks
2201 */
2202 static int asus_hotk_thaw(struct device *device)
2203 {
2204 struct asus_wmi *asus = dev_get_drvdata(device);
2205
2206 if (asus->wlan.rfkill) {
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 */
2214 wlan = asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_WLAN);
2215 asus_wmi_set_devstate(ASUS_WMI_DEVID_WLAN, wlan, NULL);
2216 }
2217
2218 return 0;
2219 }
2220
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
2231 static int asus_hotk_restore(struct device *device)
2232 {
2233 struct asus_wmi *asus = dev_get_drvdata(device);
2234 int bl;
2235
2236 /* Refresh both wlan rfkill state and pci hotplug */
2237 if (asus->wlan.rfkill)
2238 asus_rfkill_hotplug(asus);
2239
2240 if (asus->bluetooth.rfkill) {
2241 bl = !asus_wmi_get_devstate_simple(asus,
2242 ASUS_WMI_DEVID_BLUETOOTH);
2243 rfkill_set_sw_state(asus->bluetooth.rfkill, bl);
2244 }
2245 if (asus->wimax.rfkill) {
2246 bl = !asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_WIMAX);
2247 rfkill_set_sw_state(asus->wimax.rfkill, bl);
2248 }
2249 if (asus->wwan3g.rfkill) {
2250 bl = !asus_wmi_get_devstate_simple(asus, ASUS_WMI_DEVID_WWAN3G);
2251 rfkill_set_sw_state(asus->wwan3g.rfkill, bl);
2252 }
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 }
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 }
2261 if (!IS_ERR_OR_NULL(asus->kbd_led.dev))
2262 queue_work(asus->led_workqueue, &asus->kbd_led_work);
2263
2264 return 0;
2265 }
2266
2267 static const struct dev_pm_ops asus_pm_ops = {
2268 .thaw = asus_hotk_thaw,
2269 .restore = asus_hotk_restore,
2270 .resume = asus_hotk_resume,
2271 };
2272
2273 static int asus_wmi_probe(struct platform_device *pdev)
2274 {
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;
2278
2279 if (!wmi_has_guid(ASUS_WMI_MGMT_GUID)) {
2280 pr_warn("Management GUID not found\n");
2281 return -ENODEV;
2282 }
2283
2284 if (wdrv->event_guid && !wmi_has_guid(wdrv->event_guid)) {
2285 pr_warn("Event GUID not found\n");
2286 return -ENODEV;
2287 }
2288
2289 if (wdrv->probe) {
2290 ret = wdrv->probe(pdev);
2291 if (ret)
2292 return ret;
2293 }
2294
2295 return asus_wmi_add(pdev);
2296 }
2297
2298 static bool used;
2299
2300 int __init_or_module asus_wmi_register_driver(struct asus_wmi_driver *driver)
2301 {
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,
2316 NULL, 0, NULL, 0);
2317 if (IS_ERR(platform_device))
2318 return PTR_ERR(platform_device);
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)) {
2336 pr_info("Asus Management GUID not found\n");
2337 return -ENODEV;
2338 }
2339
2340 pr_info("ASUS WMI generic driver loaded\n");
2341 return 0;
2342 }
2343
2344 static void __exit asus_wmi_exit(void)
2345 {
2346 pr_info("ASUS WMI generic driver unloaded\n");
2347 }
2348
2349 module_init(asus_wmi_init);
2350 module_exit(asus_wmi_exit);