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