-
Notifications
You must be signed in to change notification settings - Fork 1
/
clevo-wmi.c
922 lines (738 loc) · 23.4 KB
/
clevo-wmi.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
/*
* CLEVO/Notebook WMI Hotkey Driver
*
* Copyright (C) 2016 Guillermo A. Amaral B. <g@maral.me>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include <linux/acpi.h>
#include <linux/delay.h>
#include <linux/dmi.h>
#include <linux/i8042.h>
#include <linux/input.h>
#include <linux/input/sparse-keymap.h>
#include <linux/kthread.h>
#include <linux/leds.h>
#include <linux/module.h>
#include <linux/mutex.h>
#include <linux/platform_device.h>
#define CLEVO_WMI_FILE "clevo"
#define CLEVO_WMI_EVENT_GUID "ABBC0F6B-8EA1-11D1-00A0-C90629100000"
#define CLEVO_WMI_METHOD_GUID "ABBC0F6D-8EA1-11D1-00A0-C90629100000"
#define CLEVO_WMI_GCMD_EVNT 0x01
#define CLEVO_WMI_SCMD_HOTKEY_ENABLE 0x46
#define CLEVO_WMI_P65_GCMD_A2_POWER 0x05
#define CLEVO_WMI_P65_GCMD_WEBCAM_POWER 0x06
#define CLEVO_WMI_P65_GCMD_BLUETOOTH_POWER 0x07
#define CLEVO_WMI_P65_GCMD_TRACKPAD_POWER 0x09
#define CLEVO_WMI_P65_GCMD_A4_POWER 0x0A
#define CLEVO_WMI_P65_GCMD_A7_POWER 0x11
#define CLEVO_WMI_P65_GCMD_KB_BACKLIGHT 0x3D
#define CLEVO_WMI_P65_GCMD_VGA 0x54 /* 0x00 = DESCRETE, 0xFF = MSHYBRID */
#define CLEVO_WMI_P65_GCMD_EC 0x73
#define CLEVO_WMI_P65_SCMD_A2_POWER 0x20
#define CLEVO_WMI_P65_SCMD_BLUETOOTH_POWER 0x21
#define CLEVO_WMI_P65_SCMD_WEBCAM_POWER 0x22
#define CLEVO_WMI_P65_SCMD_KB_BACKLIGHT 0x27
#define CLEVO_WMI_P65_SCMD_TRACKPAD_POWER 0x2A
#define CLEVO_WMI_P65_SCMD_IGNR_AC 0x48
#define CLEVO_WMI_P65_SCMD_POWER_BTN 0x49
#define CLEVO_WMI_P65_SCMD_A4_POWER 0x4C
#define CLEVO_WMI_P65_SCMD_LCD_POWER 0x5E
#define CLEVO_WMI_P65_SCMD_FAN_CONTROL 0x65
#define CLEVO_WMI_P65_SCMD_PER_FAN_SPEED 0x68 /* Seperate fan speed values (set fans to manual) */
#define CLEVO_WMI_P65_SCMD_PER_FAN_AUTO 0x69 /* Reset fan speed control to automatic (0b1111) */
#define CLEVO_WMI_P65_SCMD_INDX 0x6B
#define CLEVO_WMI_P65_SCMD_AIRPLANE_LED 0x6C
#define CLEVO_WMI_P65_SCMD_EC 0x75
#define CLEVO_WMI_P65_SCMD_ACC 0x79
#define CLEVO_I8042_SYNAPTIC_MODE 0x97
/*
* Workaround for P65, AIRP seems to flip the wrong bits.
*/
#define CLEVO_WMI_P65_OFFSET_AIRP 0xD9
#define CLEVO_WMI_P65_OFFSET_RINF 0xDB
#define CLEVO_WMI_P65_BIT_AIRP (1 << 6)
#define CLEVO_UNUSED(x) (void)x
MODULE_AUTHOR("Guillermo A. Amaral B. <g@maral.me>");
MODULE_DESCRIPTION("CLEVO/Notebook Hotkey Driver.");
MODULE_LICENSE("GPL");
MODULE_ALIAS("wmi:" CLEVO_WMI_EVENT_GUID);
enum p65_accessory_cmd_t {
P65_ACCESSORY_FAN_CONTROL = 0x01,
P65_ACCESSORY_FAN_SPEED = 0x0E,
P65_ACCESSORY_HEADPHONE_AMP = 0x0D,
P65_ACCESSORY_MSHYBRID = 0x0B,
P65_ACCESSORY_WINDOWS_KEY = 0x05
};
enum p65_keyboard_brightness_level_t {
P65_KEYBOARD_BRIGHTNESS_OFF = 0,
P65_KEYBOARD_BRIGHTNESS_20 = 1,
P65_KEYBOARD_BRIGHTNESS_40 = 2,
P65_KEYBOARD_BRIGHTNESS_60 = 3,
P65_KEYBOARD_BRIGHTNESS_80 = 4,
P65_KEYBOARD_BRIGHTNESS_MAX = 5
};
enum p65_fan_control_cmd_t {
P65_FAN_CONTROL_NORMAL = 0,
P65_FAN_CONTROL_FULL = 1,
P65_FAN_CONTROL_XTUF = 2,
_P65_FAN_CONTROL_MASK = 0x03
};
enum p65_headphone_amp_cmd_t {
P65_HEADPHONE_AMP_OFF = 0,
P65_HEADPHONE_AMP_NORMAL = 1,
P65_HEADPHONE_AMP_PRESET1 = 2,
P65_HEADPHONE_AMP_PRESET2 = 3,
P65_HEADPHONE_AMP_PRESET3 = 4, /* BOOST */
P65_HEADPHONE_AMP_PRESET4 = 5,
P65_HEADPHONE_AMP_RESET = 6,
_P65_HEADPHONE_AMP_DEFAULT = P65_HEADPHONE_AMP_PRESET3,
_P65_HEADPHONE_AMP_MAX = P65_HEADPHONE_AMP_RESET
};
static int _clevo_p65_deinit(struct platform_device *);
static int _clevo_p65_init(struct platform_device *);
static int _clevo_p65_resume(struct platform_device *);
static ssize_t _clevo_p65_show_attr(struct device *, struct device_attribute *, char *);
static ssize_t _clevo_p65_store_attr(struct device *, struct device_attribute *, const char *, size_t);
static enum led_brightness _clevo_p65_led_get(struct led_classdev *);
static void _clevo_p65_led_set(struct led_classdev *, enum led_brightness);
static void _clevo_p65_led_update(struct work_struct *);
/*
* XXX: Model abstraction is still a work in progress.
*
* I only have one CLEVO laptop so it's gonna take a while to sort out the
* common bits.
*/
static int __clevo_p65_airplane_hotkey_monitor(void *);
static int __clevo_p65_airplane_led_set(bool);
static int __clevo_p65_fan_control_set(u8);
static int __clevo_p65_fan_speed_set(u8);
static int __clevo_p65_headphone_amp_set(u8);
static int __clevo_p65_keyboard_brightness_get(u8 *);
static int __clevo_p65_keyboard_brightness_set(u8);
static int _clevo_platform_match(const struct dmi_system_id *);
static int _clevo_wmi_bool_get(int, bool *);
static int _clevo_wmi_bool_set(int, bool);
static int _clevo_wmi_evaluate_method(u32, u32, u32 *);
static void _clevo_wmi_notify(u32, void *);
static enum led_brightness _clevo_led_get(struct led_classdev *);
static void _clevo_led_set(struct led_classdev *, enum led_brightness);
static void _clevo_led_update(struct work_struct *);
static void __exit clevo_wmi_exit(void);
static int __init_or_module clevo_wmi_init(void);
static int clevo_wmi_probe(struct platform_device *);
static int clevo_wmi_remove(struct platform_device *);
static int clevo_wmi_resume(struct platform_device *);
/****************************************************************************/
struct clevo_wmi_model_t
{
int event_id;
const struct key_entry *keymap;
int (*deinit)(struct platform_device *pdev);
int (*init)(struct platform_device *pdev);
int (*resume)(struct platform_device *pdev);
enum led_brightness (*led_get)(struct led_classdev *);
void (*led_set)(struct led_classdev *, enum led_brightness);
void (*led_update)(struct work_struct *);
};
struct clevo_t
{
const struct clevo_wmi_model_t *model;
struct platform_device *pdev;
struct input_dev *idev;
/* common leds */
struct workqueue_struct *led_workqueue;
struct led_classdev airp_led;
struct work_struct airp_work;
};
static struct clevo_t s_clevo;
static DEFINE_MUTEX(s_clevo_inputdev_mutex);
static DEFINE_MUTEX(s_clevo_wmi_mutex);
static DEFINE_MUTEX(s_clevo_ec_mutex);
/*
* TODO: Replace show and store functions with wrapper that calls model
* specific versions.
*/
DEVICE_ATTR(bluetooth_power, S_IWUSR | S_IRUGO,
_clevo_p65_show_attr, _clevo_p65_store_attr);
DEVICE_ATTR(headphone_amp, S_IWUSR | S_IRUGO,
_clevo_p65_show_attr, _clevo_p65_store_attr);
DEVICE_ATTR(keyboard_backlight, S_IWUSR | S_IRUGO,
_clevo_p65_show_attr, _clevo_p65_store_attr);
DEVICE_ATTR(trackpad_power, S_IWUSR | S_IRUGO,
_clevo_p65_show_attr, _clevo_p65_store_attr);
DEVICE_ATTR(webcam_power, S_IWUSR | S_IRUGO,
_clevo_p65_show_attr, _clevo_p65_store_attr);
DEVICE_ATTR(lcd_power, S_IWUSR, NULL, _clevo_p65_store_attr);
/* CLEVO P65 ****************************************************************/
struct clevo_p65_state_t
{
int headphone_amp;
bool airplane_led;
struct task_struct *task;
};
static const struct key_entry s_clevo_p65_keymap[] = {
{ KE_KEY, 0x7B, { KEY_PROG2 } }, /* Fn+Backspace (Flexikey) */
{ KE_KEY, 0x7D, { KEY_ATTENDANT_TOGGLE } }, /* Fn+2 */
{ KE_KEY, 0x7E, { KEY_ATTENDANT_TOGGLE } }, /* Fn+2 */
{ KE_KEY, 0x8A, { KEY_KBDILLUMTOGGLE } }, /* KB Backlight */
{ KE_KEY, 0x95, { KEY_PROG1 } }, /* Fn+ESC (Control Center) */
{ KE_KEY, 0xD7, { KEY_DISPLAYTOGGLE } },
{ KE_KEY, 0xFC, { KEY_TOUCHPAD_TOGGLE } },
{ KE_KEY, 0xFD, { KEY_TOUCHPAD_TOGGLE } },
{ KE_IGNORE, 0x70, {} }, /* Fn+1 - FAN Override Off */
{ KE_IGNORE, 0x86, {} }, /* Fn+2 */
{ KE_IGNORE, 0x8F, {} }, /* Fn+1 - FAN Override On */
{ KE_IGNORE, 0xCB, {} }, /* Screen-Lock Off - Enable Meta-Key */
{ KE_IGNORE, 0xCC, {} }, /* Screen-Lock On - Disable Meta-Key */
{ KE_IGNORE, 0xC7, {} },
{ KE_IGNORE, 0xC8, {} },
{ KE_IGNORE, 0xFA, {} }, /* Volume Up/Down */
{ KE_IGNORE, 0xFB, {} }, /* Mute */
{ KE_IGNORE, 0xF6, {} }, /* Webcam Disable */
{ KE_IGNORE, 0xF7, {} }, /* Webcam Enable */
{ KE_END, 0, {} },
};
static struct clevo_wmi_model_t s_clevo_p65_model = {
.event_id = 0xD0,
.keymap = s_clevo_p65_keymap,
.deinit = _clevo_p65_deinit,
.init = _clevo_p65_init,
.resume = _clevo_p65_resume,
.led_set = _clevo_p65_led_set,
.led_get = _clevo_p65_led_get,
.led_update = _clevo_p65_led_update
};
static struct attribute * s_clevo_p65_attributes[] = {
&dev_attr_bluetooth_power.attr,
&dev_attr_headphone_amp.attr,
&dev_attr_keyboard_backlight.attr,
&dev_attr_lcd_power.attr,
&dev_attr_trackpad_power.attr,
&dev_attr_webcam_power.attr,
NULL
};
static struct attribute_group s_clevo_attribute_group = {
.attrs = s_clevo_p65_attributes
};
/****************************************************************************/
static struct platform_driver __initdata_or_module s_clevo_platform_driver = {
.remove = clevo_wmi_remove,
.resume = clevo_wmi_resume,
.driver = {
.name = CLEVO_WMI_FILE,
.owner = THIS_MODULE,
}
};
static struct dmi_system_id __initdata_or_module clevo_dmi_table[] = {
{
.ident = "P65",
.matches = {
DMI_MATCH(DMI_SYS_VENDOR, "Notebook"),
DMI_MATCH(DMI_PRODUCT_NAME, "P65_"),
},
.callback = _clevo_platform_match,
.driver_data = &s_clevo_p65_model
},
{ 0 }
};
MODULE_DEVICE_TABLE(dmi, clevo_dmi_table);
/* CLEVO P65 ****************************************************************/
int
_clevo_p65_deinit(struct platform_device *pdev)
{
struct clevo_p65_state_t *p65_state = platform_get_drvdata(pdev);
if (likely(!IS_ERR_OR_NULL(p65_state->task))) {
kthread_stop(p65_state->task);
p65_state->task = NULL;
}
sysfs_remove_group(&pdev->dev.kobj, &s_clevo_attribute_group);
kfree(p65_state);
return 0;
}
int
_clevo_p65_init(struct platform_device *pdev)
{
struct clevo_p65_state_t *p65_state;
bool tmpb = false;
p65_state = kzalloc(sizeof(struct clevo_p65_state_t), GFP_KERNEL);
p65_state->airplane_led = false;
p65_state->headphone_amp = _P65_HEADPHONE_AMP_DEFAULT;
platform_set_drvdata(pdev, p65_state);
__clevo_p65_airplane_led_set(p65_state->airplane_led);
__clevo_p65_headphone_amp_set(p65_state->headphone_amp);
/*
* Workaround for webcam power setting mismatch.
*/
if (_clevo_wmi_bool_get(CLEVO_WMI_P65_GCMD_WEBCAM_POWER, &tmpb))
_clevo_wmi_bool_set(CLEVO_WMI_P65_SCMD_WEBCAM_POWER, tmpb);
/*
* Enable touchpad hotkey.
*/
i8042_lock_chip();
i8042_command(NULL, CLEVO_I8042_SYNAPTIC_MODE);
i8042_unlock_chip();
p65_state->task =
kthread_run(__clevo_p65_airplane_hotkey_monitor, NULL,
"clevo_p65_airplane_hotkey_monitor");
if (unlikely(IS_ERR(p65_state->task))) {
p65_state->task = NULL;
pr_err("Failed to create airplane hotkey monitoring thread!\n");
return PTR_ERR(p65_state->task);
}
return sysfs_create_group(&pdev->dev.kobj, &s_clevo_attribute_group);
}
int
_clevo_p65_resume(struct platform_device *pdev)
{
struct clevo_p65_state_t *p65_state = platform_get_drvdata(pdev);
__clevo_p65_headphone_amp_set(p65_state->headphone_amp);
return 0;
}
ssize_t
_clevo_p65_show_attr(struct device *dev,
struct device_attribute *attr, char *buf)
{
struct clevo_p65_state_t *p65_state = dev_get_drvdata(dev);
bool tmpb;
u8 tmp8;
if (attr == &dev_attr_bluetooth_power) {
if (_clevo_wmi_bool_get(CLEVO_WMI_P65_GCMD_BLUETOOTH_POWER,
&tmpb))
return -EIO;
return sprintf(buf, "%i", tmpb ? 1 : 0);
}
else if (attr == &dev_attr_headphone_amp) {
return sprintf(buf, "%i", p65_state->headphone_amp);
}
else if (attr == &dev_attr_keyboard_backlight) {
if (__clevo_p65_keyboard_brightness_get(&tmp8))
return -EIO;
return sprintf(buf, "%i", tmp8);
}
else if (attr == &dev_attr_trackpad_power) {
if (_clevo_wmi_bool_get(CLEVO_WMI_P65_GCMD_TRACKPAD_POWER,
&tmpb))
return -EIO;
return sprintf(buf, "%i", tmpb ? 1 : 0);
}
else if (attr == &dev_attr_webcam_power) {
if (_clevo_wmi_bool_get(CLEVO_WMI_P65_GCMD_WEBCAM_POWER,
&tmpb))
return -EIO;
return sprintf(buf, "%i", tmpb ? 1 : 0);
}
return 0;
}
ssize_t
_clevo_p65_store_attr(struct device *dev,
struct device_attribute *attr, const char *buf, size_t count)
{
struct clevo_p65_state_t *p65_state = dev_get_drvdata(dev);
int val;
if (!count) return 0;
if (sscanf(buf, "%i", &val) != 1)
return -EINVAL;
if (attr == &dev_attr_bluetooth_power) {
if (_clevo_wmi_bool_set(CLEVO_WMI_P65_SCMD_BLUETOOTH_POWER,
val == 1))
return -EIO;
}
else if (attr == &dev_attr_headphone_amp) {
if (__clevo_p65_headphone_amp_set(val))
return -EIO;
p65_state->headphone_amp = val;
}
else if (attr == &dev_attr_keyboard_backlight) {
if (__clevo_p65_keyboard_brightness_set(val))
return -EIO;
}
else if (attr == &dev_attr_lcd_power) {
if (_clevo_wmi_bool_set(CLEVO_WMI_P65_SCMD_LCD_POWER,
val == 1))
return -EIO;
}
else if (attr == &dev_attr_trackpad_power) {
if (_clevo_wmi_bool_set(CLEVO_WMI_P65_SCMD_TRACKPAD_POWER,
val == 1))
return -EIO;
}
else if (attr == &dev_attr_webcam_power) {
if (_clevo_wmi_bool_set(CLEVO_WMI_P65_SCMD_WEBCAM_POWER,
val == 1))
return -EIO;
}
return count;
}
enum led_brightness
_clevo_p65_led_get(struct led_classdev *led)
{
struct clevo_p65_state_t *p65_state =
platform_get_drvdata(s_clevo.pdev);
if (led == &s_clevo.airp_led)
return p65_state->airplane_led ? LED_FULL : LED_OFF;
return LED_OFF;
}
void
_clevo_p65_led_set(struct led_classdev *led, enum led_brightness value)
{
struct clevo_p65_state_t *p65_state =
platform_get_drvdata(s_clevo.pdev);
if (led == &s_clevo.airp_led) {
p65_state->airplane_led = (value != LED_OFF);
queue_work(s_clevo.led_workqueue, &s_clevo.airp_work);
}
}
void
_clevo_p65_led_update(struct work_struct *work)
{
struct clevo_p65_state_t *p65_state =
platform_get_drvdata(s_clevo.pdev);
if (work == &s_clevo.airp_work) {
/* inverted, rfkill trigger treats it as a WLAN-ON led. */
__clevo_p65_airplane_led_set(!p65_state->airplane_led);
}
}
int
__clevo_p65_airplane_hotkey_monitor(void *data)
{
u32 state = 0;
pr_info("Airplane-mode key monitoring started.\n");
do {
mutex_lock(&s_clevo_ec_mutex);
if (_clevo_wmi_evaluate_method(CLEVO_WMI_P65_SCMD_INDX,
CLEVO_WMI_P65_OFFSET_RINF, NULL))
goto continue_sleep;
if(_clevo_wmi_evaluate_method(CLEVO_WMI_P65_GCMD_EC, 0, &state))
goto continue_sleep;
if (unlikely(state & CLEVO_WMI_P65_BIT_AIRP)) {
_clevo_wmi_evaluate_method(CLEVO_WMI_P65_SCMD_EC,
state & ~CLEVO_WMI_P65_BIT_AIRP, NULL);
pr_info("Airplane-mode key flag is set.\n");
mutex_lock(&s_clevo_inputdev_mutex);
input_report_key(s_clevo.idev, KEY_RFKILL, 1);
input_sync(s_clevo.idev);
input_report_key(s_clevo.idev, KEY_RFKILL, 0);
input_sync(s_clevo.idev);
mutex_unlock(&s_clevo_inputdev_mutex);
}
continue_sleep:
mutex_unlock(&s_clevo_ec_mutex);
msleep_interruptible(1000);
} while (!kthread_should_stop());
pr_info("Airplane-mode key monitoring finished.\n");
return 0;
}
int
__clevo_p65_airplane_led_set(bool on)
{
u32 state = 0;
/* This seems to pretty much do nothing.
*
* You can adjust the DSDT to update the correct offset to toggle the
* LED, but it's pretty much pointless. */
_clevo_wmi_evaluate_method(CLEVO_WMI_P65_SCMD_AIRPLANE_LED, on ? 1 : 0, NULL);
mutex_lock(&s_clevo_ec_mutex);
/*
* Update correct offset
*/
if (_clevo_wmi_evaluate_method(CLEVO_WMI_P65_SCMD_INDX,
CLEVO_WMI_P65_OFFSET_AIRP, NULL))
goto error;
if(_clevo_wmi_evaluate_method(CLEVO_WMI_P65_GCMD_EC, 0, &state))
goto error;
if (on) state |= CLEVO_WMI_P65_BIT_AIRP;
else state &= ~CLEVO_WMI_P65_BIT_AIRP;
if (_clevo_wmi_evaluate_method(CLEVO_WMI_P65_SCMD_EC, state, NULL))
goto error;
mutex_unlock(&s_clevo_ec_mutex);
return 0;
error:
mutex_unlock(&s_clevo_ec_mutex);
return -EIO;
}
int
__clevo_p65_fan_control_set(u8 ctrl)
{
if (_clevo_wmi_evaluate_method(CLEVO_WMI_P65_SCMD_ACC,
(P65_ACCESSORY_FAN_CONTROL << 24)
| (ctrl & _P65_FAN_CONTROL_MASK),
NULL))
return -EIO;
return 0;
}
int
__clevo_p65_fan_speed_set(u8 speed)
{
if (_clevo_wmi_evaluate_method(CLEVO_WMI_P65_SCMD_ACC,
(P65_ACCESSORY_FAN_SPEED << 24) | speed, NULL))
return -EIO;
return 0;
}
int
__clevo_p65_headphone_amp_set(u8 cmd)
{
if (cmd > _P65_HEADPHONE_AMP_MAX)
return -EINVAL;
if (_clevo_wmi_evaluate_method(CLEVO_WMI_P65_SCMD_ACC,
(P65_ACCESSORY_HEADPHONE_AMP << 24) | cmd, NULL))
return -EIO;
return 0;
}
int
__clevo_p65_keyboard_brightness_get(u8 *level)
{
u32 val;
if (_clevo_wmi_evaluate_method(CLEVO_WMI_P65_GCMD_KB_BACKLIGHT,
0, &val))
return -EIO;
*level = val;
return 0;
}
int
__clevo_p65_keyboard_brightness_set(u8 level)
{
if (level > P65_KEYBOARD_BRIGHTNESS_MAX)
return -EINVAL;
if (_clevo_wmi_evaluate_method(CLEVO_WMI_P65_SCMD_KB_BACKLIGHT,
level, NULL))
return -EIO;
return 0;
}
/****************************************************************************/
int
_clevo_platform_match(const struct dmi_system_id *id)
{
pr_info("CLEVO WMI detected a %s notebook.\n", id->ident);
s_clevo.model = id->driver_data;
return 1;
}
int
_clevo_wmi_bool_get(int code, bool *val)
{
u32 tmp = 0;
if (_clevo_wmi_evaluate_method(code, 0, &tmp))
return -EIO;
*val = tmp;
return 0;
}
int
_clevo_wmi_bool_set(int code, bool on)
{
if (_clevo_wmi_evaluate_method(code, on ? 1 : 0, NULL))
return -EIO;
return 0;
}
int
_clevo_wmi_evaluate_method(u32 method, u32 arg, u32 *ret)
{
struct acpi_buffer input = { (acpi_size) sizeof(u32), &arg };
struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER, NULL };
union acpi_object *obj;
acpi_status status;
u32 tmp = 0;
mutex_lock(&s_clevo_wmi_mutex);
status = wmi_evaluate_method(CLEVO_WMI_METHOD_GUID, 1,
method, &input, &output);
mutex_unlock(&s_clevo_wmi_mutex);
if (ACPI_FAILURE(status))
return -EIO;
obj = (union acpi_object *) output.pointer;
if (likely(obj)) {
if (likely(obj->type == ACPI_TYPE_INTEGER))
tmp = (u32) obj->integer.value;
else if (obj->type == ACPI_TYPE_BUFFER)
memcpy(&tmp, obj->buffer.pointer, 4);
}
if (ret) *ret = tmp;
kfree(obj);
return 0;
}
void
_clevo_wmi_notify(u32 value, void *context)
{
acpi_status status;
u32 code;
CLEVO_UNUSED(context);
if (s_clevo.model && s_clevo.model->event_id != value)
return;
code = 0;
status = _clevo_wmi_evaluate_method(CLEVO_WMI_GCMD_EVNT, 0, &code);
if (unlikely(status != AE_OK)) {
pr_err("CLEVO WMI failed to receive code for event.\n");
return;
}
mutex_lock(&s_clevo_inputdev_mutex);
if (!sparse_keymap_report_event(s_clevo.idev, code, 1, 1))
pr_info("CLEVO WMI event '%x' went unhandled.\n", code);
mutex_unlock(&s_clevo_inputdev_mutex);
}
enum led_brightness
_clevo_led_get(struct led_classdev *led)
{
if (unlikely(!s_clevo.model || !s_clevo.model->led_get))
return LED_OFF;
return s_clevo.model->led_get(led);
}
void
_clevo_led_set(struct led_classdev *led, enum led_brightness value)
{
if (unlikely(!s_clevo.model || !s_clevo.model->led_set))
return;
s_clevo.model->led_set(led, value);
}
void
_clevo_led_update(struct work_struct *work)
{
if (unlikely(!s_clevo.model || !s_clevo.model->led_update))
return;
s_clevo.model->led_update(work);
}
/****************************************************************************/
int
clevo_wmi_init(void)
{
int errno = 0;
struct platform_device *platform_device;
s_clevo.model = NULL;
platform_device = platform_create_bundle(
&s_clevo_platform_driver,
clevo_wmi_probe,
NULL, 0, NULL, 0);
if (IS_ERR(platform_device))
return PTR_ERR(platform_device);
s_clevo.pdev = platform_device;
/* setup input device */
s_clevo.idev = input_allocate_device();
if (IS_ERR_OR_NULL(s_clevo.idev)) {
errno = -ENODEV;
goto error;
}
s_clevo.idev->name = "CLEVO/Notebook Hotkey Input";
s_clevo.idev->phys = CLEVO_WMI_FILE "/input0";
s_clevo.idev->id.bustype = BUS_HOST;
s_clevo.idev->dev.parent = &platform_device->dev;
set_bit(EV_KEY, s_clevo.idev->evbit);
set_bit(KEY_ATTENDANT_TOGGLE, s_clevo.idev->keybit);
set_bit(KEY_DISPLAYTOGGLE, s_clevo.idev->keybit);
set_bit(KEY_KBDILLUMTOGGLE, s_clevo.idev->keybit);
set_bit(KEY_PROG1, s_clevo.idev->keybit);
set_bit(KEY_PROG2, s_clevo.idev->keybit);
set_bit(KEY_RFKILL, s_clevo.idev->keybit);
set_bit(KEY_TOUCHPAD_TOGGLE, s_clevo.idev->keybit);
errno = input_register_device(s_clevo.idev);
if (unlikely(errno)) goto error_idev;
if (likely(s_clevo.model && s_clevo.model->keymap)) {
errno = sparse_keymap_setup(s_clevo.idev,
s_clevo.model->keymap, NULL);
if (unlikely(errno)) goto error_idev;
}
s_clevo.led_workqueue =
create_singlethread_workqueue("led_workqueue");
if (IS_ERR_OR_NULL(s_clevo.led_workqueue)) {
errno = -ENOMEM;
#ifdef SPARCE_KEYMAP_FREE
goto error_keymap;
#else
goto error_idev;
#endif
}
if (likely(s_clevo.model->led_get &&
s_clevo.model->led_set &&
s_clevo.model->led_update)) {
INIT_WORK(&s_clevo.airp_work, _clevo_led_update);
s_clevo.airp_led.name = "clevo:green:airplane_mode";
s_clevo.airp_led.brightness_get = _clevo_led_get;
s_clevo.airp_led.brightness_set = _clevo_led_set;
s_clevo.airp_led.default_trigger = "rfkill1";
s_clevo.airp_led.max_brightness = LED_FULL;
errno = led_classdev_register(&s_clevo.pdev->dev, &s_clevo.airp_led);
if (unlikely(errno)) goto error_led_workqueue;
}
return 0;
error_led_workqueue:
destroy_workqueue(s_clevo.led_workqueue);
#ifdef SPARCE_KEYMAP_FREE
error_keymap:
sparse_keymap_free(s_clevo.idev);
#endif
error_idev:
input_free_device(s_clevo.idev);
s_clevo.idev = NULL;
error:
platform_device_unregister(platform_device);
platform_driver_unregister(&s_clevo_platform_driver);
s_clevo.pdev = NULL;
return errno;
}
void
clevo_wmi_exit(void)
{
if (likely(s_clevo.airp_led.dev))
led_classdev_unregister(&s_clevo.airp_led);
if (likely(s_clevo.led_workqueue))
destroy_workqueue(s_clevo.led_workqueue);
#ifdef SPARCE_KEYMAP_FREE
if (likely(s_clevo.model && s_clevo.model->keymap))
sparse_keymap_free(s_clevo.idev);
#endif
input_unregister_device(s_clevo.idev);
input_free_device(s_clevo.idev);
s_clevo.idev = NULL;
platform_device_unregister(s_clevo.pdev);
platform_driver_unregister(&s_clevo_platform_driver);
s_clevo.pdev = NULL;
}
int
clevo_wmi_probe(struct platform_device *pdev)
{
int ret;
if (!wmi_has_guid(CLEVO_WMI_METHOD_GUID) &&
!wmi_has_guid(CLEVO_WMI_EVENT_GUID)) {
pr_err("CLEVO WMI required GUIDs where not found.\n");
return -ENODEV;
}
if (!dmi_check_system(clevo_dmi_table))
pr_warning("CLEVO WMI unknown laptop encountered.\n");
ret = wmi_install_notify_handler(CLEVO_WMI_EVENT_GUID,
_clevo_wmi_notify, NULL);
if (ACPI_FAILURE(ret)) {
pr_err("CLEVO WMI was unable to install notification handler.\n");
return -ENODEV;
}
_clevo_wmi_evaluate_method(CLEVO_WMI_SCMD_HOTKEY_ENABLE, 0, NULL);
if (likely(s_clevo.model && s_clevo.model->init))
return s_clevo.model->init(pdev);
return 0;
}
int
clevo_wmi_remove(struct platform_device *pdev)
{
wmi_remove_notify_handler(CLEVO_WMI_EVENT_GUID);
if (likely(s_clevo.model && s_clevo.model->deinit))
s_clevo.model->deinit(pdev);
return 0;
}
int
clevo_wmi_resume(struct platform_device *pdev)
{
_clevo_wmi_evaluate_method(CLEVO_WMI_SCMD_HOTKEY_ENABLE, 0, NULL);
if (likely(s_clevo.model && s_clevo.model->resume))
s_clevo.model->resume(pdev);
return 0;
}
module_init(clevo_wmi_init);
module_exit(clevo_wmi_exit);