forked from torvalds/linux
-
Notifications
You must be signed in to change notification settings - Fork 0
/
asus_atk0110.c
1457 lines (1215 loc) · 33 KB
/
asus_atk0110.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
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (C) 2007-2009 Luca Tettamanti <[email protected]>
*
* This file is released under the GPLv2
* See COPYING in the top level directory of the kernel tree.
*/
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
#include <linux/debugfs.h>
#include <linux/kernel.h>
#include <linux/hwmon.h>
#include <linux/list.h>
#include <linux/module.h>
#include <linux/slab.h>
#include <linux/dmi.h>
#include <acpi/acpi.h>
#include <acpi/acpixf.h>
#include <acpi/acpi_drivers.h>
#include <acpi/acpi_bus.h>
#define ATK_HID "ATK0110"
static bool new_if;
module_param(new_if, bool, 0);
MODULE_PARM_DESC(new_if, "Override detection heuristic and force the use of the new ATK0110 interface");
static const struct dmi_system_id __initconst atk_force_new_if[] = {
{
/* Old interface has broken MCH temp monitoring */
.ident = "Asus Sabertooth X58",
.matches = {
DMI_MATCH(DMI_BOARD_NAME, "SABERTOOTH X58")
}
},
{ }
};
/* Minimum time between readings, enforced in order to avoid
* hogging the CPU.
*/
#define CACHE_TIME HZ
#define BOARD_ID "MBIF"
#define METHOD_ENUMERATE "GGRP"
#define METHOD_READ "GITM"
#define METHOD_WRITE "SITM"
#define METHOD_OLD_READ_TMP "RTMP"
#define METHOD_OLD_READ_VLT "RVLT"
#define METHOD_OLD_READ_FAN "RFAN"
#define METHOD_OLD_ENUM_TMP "TSIF"
#define METHOD_OLD_ENUM_VLT "VSIF"
#define METHOD_OLD_ENUM_FAN "FSIF"
#define ATK_MUX_HWMON 0x00000006ULL
#define ATK_MUX_MGMT 0x00000011ULL
#define ATK_CLASS_MASK 0xff000000ULL
#define ATK_CLASS_FREQ_CTL 0x03000000ULL
#define ATK_CLASS_FAN_CTL 0x04000000ULL
#define ATK_CLASS_HWMON 0x06000000ULL
#define ATK_CLASS_MGMT 0x11000000ULL
#define ATK_TYPE_MASK 0x00ff0000ULL
#define HWMON_TYPE_VOLT 0x00020000ULL
#define HWMON_TYPE_TEMP 0x00030000ULL
#define HWMON_TYPE_FAN 0x00040000ULL
#define ATK_ELEMENT_ID_MASK 0x0000ffffULL
#define ATK_EC_ID 0x11060004ULL
enum atk_pack_member {
HWMON_PACK_FLAGS,
HWMON_PACK_NAME,
HWMON_PACK_LIMIT1,
HWMON_PACK_LIMIT2,
HWMON_PACK_ENABLE
};
/* New package format */
#define _HWMON_NEW_PACK_SIZE 7
#define _HWMON_NEW_PACK_FLAGS 0
#define _HWMON_NEW_PACK_NAME 1
#define _HWMON_NEW_PACK_UNK1 2
#define _HWMON_NEW_PACK_UNK2 3
#define _HWMON_NEW_PACK_LIMIT1 4
#define _HWMON_NEW_PACK_LIMIT2 5
#define _HWMON_NEW_PACK_ENABLE 6
/* Old package format */
#define _HWMON_OLD_PACK_SIZE 5
#define _HWMON_OLD_PACK_FLAGS 0
#define _HWMON_OLD_PACK_NAME 1
#define _HWMON_OLD_PACK_LIMIT1 2
#define _HWMON_OLD_PACK_LIMIT2 3
#define _HWMON_OLD_PACK_ENABLE 4
struct atk_data {
struct device *hwmon_dev;
acpi_handle atk_handle;
struct acpi_device *acpi_dev;
bool old_interface;
/* old interface */
acpi_handle rtmp_handle;
acpi_handle rvlt_handle;
acpi_handle rfan_handle;
/* new inteface */
acpi_handle enumerate_handle;
acpi_handle read_handle;
acpi_handle write_handle;
bool disable_ec;
int voltage_count;
int temperature_count;
int fan_count;
struct list_head sensor_list;
struct {
struct dentry *root;
u32 id;
} debugfs;
};
typedef ssize_t (*sysfs_show_func)(struct device *dev,
struct device_attribute *attr, char *buf);
static const struct acpi_device_id atk_ids[] = {
{ATK_HID, 0},
{"", 0},
};
MODULE_DEVICE_TABLE(acpi, atk_ids);
#define ATTR_NAME_SIZE 16 /* Worst case is "tempN_input" */
struct atk_sensor_data {
struct list_head list;
struct atk_data *data;
struct device_attribute label_attr;
struct device_attribute input_attr;
struct device_attribute limit1_attr;
struct device_attribute limit2_attr;
char label_attr_name[ATTR_NAME_SIZE];
char input_attr_name[ATTR_NAME_SIZE];
char limit1_attr_name[ATTR_NAME_SIZE];
char limit2_attr_name[ATTR_NAME_SIZE];
u64 id;
u64 type;
u64 limit1;
u64 limit2;
u64 cached_value;
unsigned long last_updated; /* in jiffies */
bool is_valid;
char const *acpi_name;
};
/* Return buffer format:
* [0-3] "value" is valid flag
* [4-7] value
* [8- ] unknown stuff on newer mobos
*/
struct atk_acpi_ret_buffer {
u32 flags;
u32 value;
u8 data[];
};
/* Input buffer used for GITM and SITM methods */
struct atk_acpi_input_buf {
u32 id;
u32 param1;
u32 param2;
};
static int atk_add(struct acpi_device *device);
static int atk_remove(struct acpi_device *device, int type);
static void atk_print_sensor(struct atk_data *data, union acpi_object *obj);
static int atk_read_value(struct atk_sensor_data *sensor, u64 *value);
static void atk_free_sensors(struct atk_data *data);
static struct acpi_driver atk_driver = {
.name = ATK_HID,
.class = "hwmon",
.ids = atk_ids,
.ops = {
.add = atk_add,
.remove = atk_remove,
},
};
#define input_to_atk_sensor(attr) \
container_of(attr, struct atk_sensor_data, input_attr)
#define label_to_atk_sensor(attr) \
container_of(attr, struct atk_sensor_data, label_attr)
#define limit1_to_atk_sensor(attr) \
container_of(attr, struct atk_sensor_data, limit1_attr)
#define limit2_to_atk_sensor(attr) \
container_of(attr, struct atk_sensor_data, limit2_attr)
static ssize_t atk_input_show(struct device *dev,
struct device_attribute *attr, char *buf)
{
struct atk_sensor_data *s = input_to_atk_sensor(attr);
u64 value;
int err;
err = atk_read_value(s, &value);
if (err)
return err;
if (s->type == HWMON_TYPE_TEMP)
/* ACPI returns decidegree */
value *= 100;
return sprintf(buf, "%llu\n", value);
}
static ssize_t atk_label_show(struct device *dev,
struct device_attribute *attr, char *buf)
{
struct atk_sensor_data *s = label_to_atk_sensor(attr);
return sprintf(buf, "%s\n", s->acpi_name);
}
static ssize_t atk_limit1_show(struct device *dev,
struct device_attribute *attr, char *buf)
{
struct atk_sensor_data *s = limit1_to_atk_sensor(attr);
u64 value = s->limit1;
if (s->type == HWMON_TYPE_TEMP)
value *= 100;
return sprintf(buf, "%lld\n", value);
}
static ssize_t atk_limit2_show(struct device *dev,
struct device_attribute *attr, char *buf)
{
struct atk_sensor_data *s = limit2_to_atk_sensor(attr);
u64 value = s->limit2;
if (s->type == HWMON_TYPE_TEMP)
value *= 100;
return sprintf(buf, "%lld\n", value);
}
static ssize_t atk_name_show(struct device *dev,
struct device_attribute *attr, char *buf)
{
return sprintf(buf, "atk0110\n");
}
static struct device_attribute atk_name_attr =
__ATTR(name, 0444, atk_name_show, NULL);
static void atk_init_attribute(struct device_attribute *attr, char *name,
sysfs_show_func show)
{
sysfs_attr_init(&attr->attr);
attr->attr.name = name;
attr->attr.mode = 0444;
attr->show = show;
attr->store = NULL;
}
static union acpi_object *atk_get_pack_member(struct atk_data *data,
union acpi_object *pack,
enum atk_pack_member m)
{
bool old_if = data->old_interface;
int offset;
switch (m) {
case HWMON_PACK_FLAGS:
offset = old_if ? _HWMON_OLD_PACK_FLAGS : _HWMON_NEW_PACK_FLAGS;
break;
case HWMON_PACK_NAME:
offset = old_if ? _HWMON_OLD_PACK_NAME : _HWMON_NEW_PACK_NAME;
break;
case HWMON_PACK_LIMIT1:
offset = old_if ? _HWMON_OLD_PACK_LIMIT1 :
_HWMON_NEW_PACK_LIMIT1;
break;
case HWMON_PACK_LIMIT2:
offset = old_if ? _HWMON_OLD_PACK_LIMIT2 :
_HWMON_NEW_PACK_LIMIT2;
break;
case HWMON_PACK_ENABLE:
offset = old_if ? _HWMON_OLD_PACK_ENABLE :
_HWMON_NEW_PACK_ENABLE;
break;
default:
return NULL;
}
return &pack->package.elements[offset];
}
/* New package format is:
* - flag (int)
* class - used for de-muxing the request to the correct GITn
* type (volt, temp, fan)
* sensor id |
* sensor id - used for de-muxing the request _inside_ the GITn
* - name (str)
* - unknown (int)
* - unknown (int)
* - limit1 (int)
* - limit2 (int)
* - enable (int)
*
* The old package has the same format but it's missing the two unknown fields.
*/
static int validate_hwmon_pack(struct atk_data *data, union acpi_object *obj)
{
struct device *dev = &data->acpi_dev->dev;
union acpi_object *tmp;
bool old_if = data->old_interface;
int const expected_size = old_if ? _HWMON_OLD_PACK_SIZE :
_HWMON_NEW_PACK_SIZE;
if (obj->type != ACPI_TYPE_PACKAGE) {
dev_warn(dev, "Invalid type: %d\n", obj->type);
return -EINVAL;
}
if (obj->package.count != expected_size) {
dev_warn(dev, "Invalid package size: %d, expected: %d\n",
obj->package.count, expected_size);
return -EINVAL;
}
tmp = atk_get_pack_member(data, obj, HWMON_PACK_FLAGS);
if (tmp->type != ACPI_TYPE_INTEGER) {
dev_warn(dev, "Invalid type (flag): %d\n", tmp->type);
return -EINVAL;
}
tmp = atk_get_pack_member(data, obj, HWMON_PACK_NAME);
if (tmp->type != ACPI_TYPE_STRING) {
dev_warn(dev, "Invalid type (name): %d\n", tmp->type);
return -EINVAL;
}
/* Don't check... we don't know what they're useful for anyway */
#if 0
tmp = &obj->package.elements[HWMON_PACK_UNK1];
if (tmp->type != ACPI_TYPE_INTEGER) {
dev_warn(dev, "Invalid type (unk1): %d\n", tmp->type);
return -EINVAL;
}
tmp = &obj->package.elements[HWMON_PACK_UNK2];
if (tmp->type != ACPI_TYPE_INTEGER) {
dev_warn(dev, "Invalid type (unk2): %d\n", tmp->type);
return -EINVAL;
}
#endif
tmp = atk_get_pack_member(data, obj, HWMON_PACK_LIMIT1);
if (tmp->type != ACPI_TYPE_INTEGER) {
dev_warn(dev, "Invalid type (limit1): %d\n", tmp->type);
return -EINVAL;
}
tmp = atk_get_pack_member(data, obj, HWMON_PACK_LIMIT2);
if (tmp->type != ACPI_TYPE_INTEGER) {
dev_warn(dev, "Invalid type (limit2): %d\n", tmp->type);
return -EINVAL;
}
tmp = atk_get_pack_member(data, obj, HWMON_PACK_ENABLE);
if (tmp->type != ACPI_TYPE_INTEGER) {
dev_warn(dev, "Invalid type (enable): %d\n", tmp->type);
return -EINVAL;
}
atk_print_sensor(data, obj);
return 0;
}
#ifdef DEBUG
static char const *atk_sensor_type(union acpi_object *flags)
{
u64 type = flags->integer.value & ATK_TYPE_MASK;
char const *what;
switch (type) {
case HWMON_TYPE_VOLT:
what = "voltage";
break;
case HWMON_TYPE_TEMP:
what = "temperature";
break;
case HWMON_TYPE_FAN:
what = "fan";
break;
default:
what = "unknown";
break;
}
return what;
}
#endif
static void atk_print_sensor(struct atk_data *data, union acpi_object *obj)
{
#ifdef DEBUG
struct device *dev = &data->acpi_dev->dev;
union acpi_object *flags;
union acpi_object *name;
union acpi_object *limit1;
union acpi_object *limit2;
union acpi_object *enable;
char const *what;
flags = atk_get_pack_member(data, obj, HWMON_PACK_FLAGS);
name = atk_get_pack_member(data, obj, HWMON_PACK_NAME);
limit1 = atk_get_pack_member(data, obj, HWMON_PACK_LIMIT1);
limit2 = atk_get_pack_member(data, obj, HWMON_PACK_LIMIT2);
enable = atk_get_pack_member(data, obj, HWMON_PACK_ENABLE);
what = atk_sensor_type(flags);
dev_dbg(dev, "%s: %#llx %s [%llu-%llu] %s\n", what,
flags->integer.value,
name->string.pointer,
limit1->integer.value, limit2->integer.value,
enable->integer.value ? "enabled" : "disabled");
#endif
}
static int atk_read_value_old(struct atk_sensor_data *sensor, u64 *value)
{
struct atk_data *data = sensor->data;
struct device *dev = &data->acpi_dev->dev;
struct acpi_object_list params;
union acpi_object id;
acpi_status status;
acpi_handle method;
switch (sensor->type) {
case HWMON_TYPE_VOLT:
method = data->rvlt_handle;
break;
case HWMON_TYPE_TEMP:
method = data->rtmp_handle;
break;
case HWMON_TYPE_FAN:
method = data->rfan_handle;
break;
default:
return -EINVAL;
}
id.type = ACPI_TYPE_INTEGER;
id.integer.value = sensor->id;
params.count = 1;
params.pointer = &id;
status = acpi_evaluate_integer(method, NULL, ¶ms, value);
if (status != AE_OK) {
dev_warn(dev, "%s: ACPI exception: %s\n", __func__,
acpi_format_exception(status));
return -EIO;
}
return 0;
}
static union acpi_object *atk_ggrp(struct atk_data *data, u16 mux)
{
struct device *dev = &data->acpi_dev->dev;
struct acpi_buffer buf;
acpi_status ret;
struct acpi_object_list params;
union acpi_object id;
union acpi_object *pack;
id.type = ACPI_TYPE_INTEGER;
id.integer.value = mux;
params.count = 1;
params.pointer = &id;
buf.length = ACPI_ALLOCATE_BUFFER;
ret = acpi_evaluate_object(data->enumerate_handle, NULL, ¶ms, &buf);
if (ret != AE_OK) {
dev_err(dev, "GGRP[%#x] ACPI exception: %s\n", mux,
acpi_format_exception(ret));
return ERR_PTR(-EIO);
}
pack = buf.pointer;
if (pack->type != ACPI_TYPE_PACKAGE) {
/* Execution was successful, but the id was not found */
ACPI_FREE(pack);
return ERR_PTR(-ENOENT);
}
if (pack->package.count < 1) {
dev_err(dev, "GGRP[%#x] package is too small\n", mux);
ACPI_FREE(pack);
return ERR_PTR(-EIO);
}
return pack;
}
static union acpi_object *atk_gitm(struct atk_data *data, u64 id)
{
struct device *dev = &data->acpi_dev->dev;
struct atk_acpi_input_buf buf;
union acpi_object tmp;
struct acpi_object_list params;
struct acpi_buffer ret;
union acpi_object *obj;
acpi_status status;
buf.id = id;
buf.param1 = 0;
buf.param2 = 0;
tmp.type = ACPI_TYPE_BUFFER;
tmp.buffer.pointer = (u8 *)&buf;
tmp.buffer.length = sizeof(buf);
params.count = 1;
params.pointer = (void *)&tmp;
ret.length = ACPI_ALLOCATE_BUFFER;
status = acpi_evaluate_object_typed(data->read_handle, NULL, ¶ms,
&ret, ACPI_TYPE_BUFFER);
if (status != AE_OK) {
dev_warn(dev, "GITM[%#llx] ACPI exception: %s\n", id,
acpi_format_exception(status));
return ERR_PTR(-EIO);
}
obj = ret.pointer;
/* Sanity check */
if (obj->buffer.length < 8) {
dev_warn(dev, "Unexpected ASBF length: %u\n",
obj->buffer.length);
ACPI_FREE(obj);
return ERR_PTR(-EIO);
}
return obj;
}
static union acpi_object *atk_sitm(struct atk_data *data,
struct atk_acpi_input_buf *buf)
{
struct device *dev = &data->acpi_dev->dev;
struct acpi_object_list params;
union acpi_object tmp;
struct acpi_buffer ret;
union acpi_object *obj;
acpi_status status;
tmp.type = ACPI_TYPE_BUFFER;
tmp.buffer.pointer = (u8 *)buf;
tmp.buffer.length = sizeof(*buf);
params.count = 1;
params.pointer = &tmp;
ret.length = ACPI_ALLOCATE_BUFFER;
status = acpi_evaluate_object_typed(data->write_handle, NULL, ¶ms,
&ret, ACPI_TYPE_BUFFER);
if (status != AE_OK) {
dev_warn(dev, "SITM[%#x] ACPI exception: %s\n", buf->id,
acpi_format_exception(status));
return ERR_PTR(-EIO);
}
obj = ret.pointer;
/* Sanity check */
if (obj->buffer.length < 8) {
dev_warn(dev, "Unexpected ASBF length: %u\n",
obj->buffer.length);
ACPI_FREE(obj);
return ERR_PTR(-EIO);
}
return obj;
}
static int atk_read_value_new(struct atk_sensor_data *sensor, u64 *value)
{
struct atk_data *data = sensor->data;
struct device *dev = &data->acpi_dev->dev;
union acpi_object *obj;
struct atk_acpi_ret_buffer *buf;
int err = 0;
obj = atk_gitm(data, sensor->id);
if (IS_ERR(obj))
return PTR_ERR(obj);
buf = (struct atk_acpi_ret_buffer *)obj->buffer.pointer;
if (buf->flags == 0) {
/* The reading is not valid, possible causes:
* - sensor failure
* - enumeration was FUBAR (and we didn't notice)
*/
dev_warn(dev, "Read failed, sensor = %#llx\n", sensor->id);
err = -EIO;
goto out;
}
*value = buf->value;
out:
ACPI_FREE(obj);
return err;
}
static int atk_read_value(struct atk_sensor_data *sensor, u64 *value)
{
int err;
if (!sensor->is_valid ||
time_after(jiffies, sensor->last_updated + CACHE_TIME)) {
if (sensor->data->old_interface)
err = atk_read_value_old(sensor, value);
else
err = atk_read_value_new(sensor, value);
sensor->is_valid = true;
sensor->last_updated = jiffies;
sensor->cached_value = *value;
} else {
*value = sensor->cached_value;
err = 0;
}
return err;
}
#ifdef CONFIG_DEBUG_FS
static int atk_debugfs_gitm_get(void *p, u64 *val)
{
struct atk_data *data = p;
union acpi_object *ret;
struct atk_acpi_ret_buffer *buf;
int err = 0;
if (!data->read_handle)
return -ENODEV;
if (!data->debugfs.id)
return -EINVAL;
ret = atk_gitm(data, data->debugfs.id);
if (IS_ERR(ret))
return PTR_ERR(ret);
buf = (struct atk_acpi_ret_buffer *)ret->buffer.pointer;
if (buf->flags)
*val = buf->value;
else
err = -EIO;
ACPI_FREE(ret);
return err;
}
DEFINE_SIMPLE_ATTRIBUTE(atk_debugfs_gitm,
atk_debugfs_gitm_get,
NULL,
"0x%08llx\n")
static int atk_acpi_print(char *buf, size_t sz, union acpi_object *obj)
{
int ret = 0;
switch (obj->type) {
case ACPI_TYPE_INTEGER:
ret = snprintf(buf, sz, "0x%08llx\n", obj->integer.value);
break;
case ACPI_TYPE_STRING:
ret = snprintf(buf, sz, "%s\n", obj->string.pointer);
break;
}
return ret;
}
static void atk_pack_print(char *buf, size_t sz, union acpi_object *pack)
{
int ret;
int i;
for (i = 0; i < pack->package.count; i++) {
union acpi_object *obj = &pack->package.elements[i];
ret = atk_acpi_print(buf, sz, obj);
if (ret >= sz)
break;
buf += ret;
sz -= ret;
}
}
static int atk_debugfs_ggrp_open(struct inode *inode, struct file *file)
{
struct atk_data *data = inode->i_private;
char *buf = NULL;
union acpi_object *ret;
u8 cls;
int i;
if (!data->enumerate_handle)
return -ENODEV;
if (!data->debugfs.id)
return -EINVAL;
cls = (data->debugfs.id & 0xff000000) >> 24;
ret = atk_ggrp(data, cls);
if (IS_ERR(ret))
return PTR_ERR(ret);
for (i = 0; i < ret->package.count; i++) {
union acpi_object *pack = &ret->package.elements[i];
union acpi_object *id;
if (pack->type != ACPI_TYPE_PACKAGE)
continue;
if (!pack->package.count)
continue;
id = &pack->package.elements[0];
if (id->integer.value == data->debugfs.id) {
/* Print the package */
buf = kzalloc(512, GFP_KERNEL);
if (!buf) {
ACPI_FREE(ret);
return -ENOMEM;
}
atk_pack_print(buf, 512, pack);
break;
}
}
ACPI_FREE(ret);
if (!buf)
return -EINVAL;
file->private_data = buf;
return nonseekable_open(inode, file);
}
static ssize_t atk_debugfs_ggrp_read(struct file *file, char __user *buf,
size_t count, loff_t *pos)
{
char *str = file->private_data;
size_t len = strlen(str);
return simple_read_from_buffer(buf, count, pos, str, len);
}
static int atk_debugfs_ggrp_release(struct inode *inode, struct file *file)
{
kfree(file->private_data);
return 0;
}
static const struct file_operations atk_debugfs_ggrp_fops = {
.read = atk_debugfs_ggrp_read,
.open = atk_debugfs_ggrp_open,
.release = atk_debugfs_ggrp_release,
.llseek = no_llseek,
};
static void atk_debugfs_init(struct atk_data *data)
{
struct dentry *d;
struct dentry *f;
data->debugfs.id = 0;
d = debugfs_create_dir("asus_atk0110", NULL);
if (!d || IS_ERR(d))
return;
f = debugfs_create_x32("id", S_IRUSR | S_IWUSR, d, &data->debugfs.id);
if (!f || IS_ERR(f))
goto cleanup;
f = debugfs_create_file("gitm", S_IRUSR, d, data,
&atk_debugfs_gitm);
if (!f || IS_ERR(f))
goto cleanup;
f = debugfs_create_file("ggrp", S_IRUSR, d, data,
&atk_debugfs_ggrp_fops);
if (!f || IS_ERR(f))
goto cleanup;
data->debugfs.root = d;
return;
cleanup:
debugfs_remove_recursive(d);
}
static void atk_debugfs_cleanup(struct atk_data *data)
{
debugfs_remove_recursive(data->debugfs.root);
}
#else /* CONFIG_DEBUG_FS */
static void atk_debugfs_init(struct atk_data *data)
{
}
static void atk_debugfs_cleanup(struct atk_data *data)
{
}
#endif
static int atk_add_sensor(struct atk_data *data, union acpi_object *obj)
{
struct device *dev = &data->acpi_dev->dev;
union acpi_object *flags;
union acpi_object *name;
union acpi_object *limit1;
union acpi_object *limit2;
union acpi_object *enable;
struct atk_sensor_data *sensor;
char const *base_name;
char const *limit1_name;
char const *limit2_name;
u64 type;
int err;
int *num;
int start;
if (obj->type != ACPI_TYPE_PACKAGE) {
/* wft is this? */
dev_warn(dev, "Unknown type for ACPI object: (%d)\n",
obj->type);
return -EINVAL;
}
err = validate_hwmon_pack(data, obj);
if (err)
return err;
/* Ok, we have a valid hwmon package */
type = atk_get_pack_member(data, obj, HWMON_PACK_FLAGS)->integer.value
& ATK_TYPE_MASK;
switch (type) {
case HWMON_TYPE_VOLT:
base_name = "in";
limit1_name = "min";
limit2_name = "max";
num = &data->voltage_count;
start = 0;
break;
case HWMON_TYPE_TEMP:
base_name = "temp";
limit1_name = "max";
limit2_name = "crit";
num = &data->temperature_count;
start = 1;
break;
case HWMON_TYPE_FAN:
base_name = "fan";
limit1_name = "min";
limit2_name = "max";
num = &data->fan_count;
start = 1;
break;
default:
dev_warn(dev, "Unknown sensor type: %#llx\n", type);
return -EINVAL;
}
enable = atk_get_pack_member(data, obj, HWMON_PACK_ENABLE);
if (!enable->integer.value)
/* sensor is disabled */
return 0;
flags = atk_get_pack_member(data, obj, HWMON_PACK_FLAGS);
name = atk_get_pack_member(data, obj, HWMON_PACK_NAME);
limit1 = atk_get_pack_member(data, obj, HWMON_PACK_LIMIT1);
limit2 = atk_get_pack_member(data, obj, HWMON_PACK_LIMIT2);
sensor = kzalloc(sizeof(*sensor), GFP_KERNEL);
if (!sensor)
return -ENOMEM;
sensor->acpi_name = kstrdup(name->string.pointer, GFP_KERNEL);
if (!sensor->acpi_name) {
err = -ENOMEM;
goto out;
}
INIT_LIST_HEAD(&sensor->list);
sensor->type = type;
sensor->data = data;
sensor->id = flags->integer.value;
sensor->limit1 = limit1->integer.value;
if (data->old_interface)
sensor->limit2 = limit2->integer.value;
else
/* The upper limit is expressed as delta from lower limit */
sensor->limit2 = sensor->limit1 + limit2->integer.value;
snprintf(sensor->input_attr_name, ATTR_NAME_SIZE,
"%s%d_input", base_name, start + *num);
atk_init_attribute(&sensor->input_attr,
sensor->input_attr_name,
atk_input_show);
snprintf(sensor->label_attr_name, ATTR_NAME_SIZE,
"%s%d_label", base_name, start + *num);
atk_init_attribute(&sensor->label_attr,
sensor->label_attr_name,
atk_label_show);
snprintf(sensor->limit1_attr_name, ATTR_NAME_SIZE,
"%s%d_%s", base_name, start + *num, limit1_name);
atk_init_attribute(&sensor->limit1_attr,
sensor->limit1_attr_name,
atk_limit1_show);
snprintf(sensor->limit2_attr_name, ATTR_NAME_SIZE,
"%s%d_%s", base_name, start + *num, limit2_name);
atk_init_attribute(&sensor->limit2_attr,
sensor->limit2_attr_name,
atk_limit2_show);
list_add(&sensor->list, &data->sensor_list);
(*num)++;
return 1;
out:
kfree(sensor->acpi_name);
kfree(sensor);
return err;
}
static int atk_enumerate_old_hwmon(struct atk_data *data)
{
struct device *dev = &data->acpi_dev->dev;
struct acpi_buffer buf;
union acpi_object *pack;
acpi_status status;
int i, ret;
int count = 0;
/* Voltages */
buf.length = ACPI_ALLOCATE_BUFFER;
status = acpi_evaluate_object_typed(data->atk_handle,
METHOD_OLD_ENUM_VLT, NULL, &buf, ACPI_TYPE_PACKAGE);
if (status != AE_OK) {
dev_warn(dev, METHOD_OLD_ENUM_VLT ": ACPI exception: %s\n",
acpi_format_exception(status));
return -ENODEV;
}
pack = buf.pointer;
for (i = 1; i < pack->package.count; i++) {
union acpi_object *obj = &pack->package.elements[i];
ret = atk_add_sensor(data, obj);
if (ret > 0)
count++;
}
ACPI_FREE(buf.pointer);
/* Temperatures */
buf.length = ACPI_ALLOCATE_BUFFER;
status = acpi_evaluate_object_typed(data->atk_handle,
METHOD_OLD_ENUM_TMP, NULL, &buf, ACPI_TYPE_PACKAGE);
if (status != AE_OK) {
dev_warn(dev, METHOD_OLD_ENUM_TMP ": ACPI exception: %s\n",
acpi_format_exception(status));
ret = -ENODEV;
goto cleanup;
}