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