1
2
3
4
5
6
7
8#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
9
10#include <linux/debugfs.h>
11#include <linux/kernel.h>
12#include <linux/hwmon.h>
13#include <linux/list.h>
14#include <linux/module.h>
15#include <linux/slab.h>
16#include <linux/dmi.h>
17#include <linux/jiffies.h>
18#include <linux/err.h>
19
20#include <acpi/acpi.h>
21#include <acpi/acpi_drivers.h>
22#include <acpi/acpi_bus.h>
23
24
25#define ATK_HID "ATK0110"
26
27static bool new_if;
28module_param(new_if, bool, 0);
29MODULE_PARM_DESC(new_if, "Override detection heuristic and force the use of the new ATK0110 interface");
30
31static const struct dmi_system_id __initconst atk_force_new_if[] = {
32 {
33
34 .ident = "Asus Sabertooth X58",
35 .matches = {
36 DMI_MATCH(DMI_BOARD_NAME, "SABERTOOTH X58")
37 }
38 }, {
39
40 .ident = "Asus M5A78L",
41 .matches = {
42 DMI_MATCH(DMI_BOARD_NAME, "M5A78L")
43 }
44 },
45 { }
46};
47
48
49
50
51
52#define CACHE_TIME HZ
53
54#define BOARD_ID "MBIF"
55#define METHOD_ENUMERATE "GGRP"
56#define METHOD_READ "GITM"
57#define METHOD_WRITE "SITM"
58#define METHOD_OLD_READ_TMP "RTMP"
59#define METHOD_OLD_READ_VLT "RVLT"
60#define METHOD_OLD_READ_FAN "RFAN"
61#define METHOD_OLD_ENUM_TMP "TSIF"
62#define METHOD_OLD_ENUM_VLT "VSIF"
63#define METHOD_OLD_ENUM_FAN "FSIF"
64
65#define ATK_MUX_HWMON 0x00000006ULL
66#define ATK_MUX_MGMT 0x00000011ULL
67
68#define ATK_CLASS_MASK 0xff000000ULL
69#define ATK_CLASS_FREQ_CTL 0x03000000ULL
70#define ATK_CLASS_FAN_CTL 0x04000000ULL
71#define ATK_CLASS_HWMON 0x06000000ULL
72#define ATK_CLASS_MGMT 0x11000000ULL
73
74#define ATK_TYPE_MASK 0x00ff0000ULL
75#define HWMON_TYPE_VOLT 0x00020000ULL
76#define HWMON_TYPE_TEMP 0x00030000ULL
77#define HWMON_TYPE_FAN 0x00040000ULL
78
79#define ATK_ELEMENT_ID_MASK 0x0000ffffULL
80
81#define ATK_EC_ID 0x11060004ULL
82
83enum atk_pack_member {
84 HWMON_PACK_FLAGS,
85 HWMON_PACK_NAME,
86 HWMON_PACK_LIMIT1,
87 HWMON_PACK_LIMIT2,
88 HWMON_PACK_ENABLE
89};
90
91
92#define _HWMON_NEW_PACK_SIZE 7
93#define _HWMON_NEW_PACK_FLAGS 0
94#define _HWMON_NEW_PACK_NAME 1
95#define _HWMON_NEW_PACK_UNK1 2
96#define _HWMON_NEW_PACK_UNK2 3
97#define _HWMON_NEW_PACK_LIMIT1 4
98#define _HWMON_NEW_PACK_LIMIT2 5
99#define _HWMON_NEW_PACK_ENABLE 6
100
101
102#define _HWMON_OLD_PACK_SIZE 5
103#define _HWMON_OLD_PACK_FLAGS 0
104#define _HWMON_OLD_PACK_NAME 1
105#define _HWMON_OLD_PACK_LIMIT1 2
106#define _HWMON_OLD_PACK_LIMIT2 3
107#define _HWMON_OLD_PACK_ENABLE 4
108
109
110struct atk_data {
111 struct device *hwmon_dev;
112 acpi_handle atk_handle;
113 struct acpi_device *acpi_dev;
114
115 bool old_interface;
116
117
118 acpi_handle rtmp_handle;
119 acpi_handle rvlt_handle;
120 acpi_handle rfan_handle;
121
122 acpi_handle enumerate_handle;
123 acpi_handle read_handle;
124 acpi_handle write_handle;
125
126 bool disable_ec;
127
128 int voltage_count;
129 int temperature_count;
130 int fan_count;
131 struct list_head sensor_list;
132
133 struct {
134 struct dentry *root;
135 u32 id;
136 } debugfs;
137};
138
139
140typedef ssize_t (*sysfs_show_func)(struct device *dev,
141 struct device_attribute *attr, char *buf);
142
143static const struct acpi_device_id atk_ids[] = {
144 {ATK_HID, 0},
145 {"", 0},
146};
147MODULE_DEVICE_TABLE(acpi, atk_ids);
148
149#define ATTR_NAME_SIZE 16
150
151struct atk_sensor_data {
152 struct list_head list;
153 struct atk_data *data;
154 struct device_attribute label_attr;
155 struct device_attribute input_attr;
156 struct device_attribute limit1_attr;
157 struct device_attribute limit2_attr;
158 char label_attr_name[ATTR_NAME_SIZE];
159 char input_attr_name[ATTR_NAME_SIZE];
160 char limit1_attr_name[ATTR_NAME_SIZE];
161 char limit2_attr_name[ATTR_NAME_SIZE];
162 u64 id;
163 u64 type;
164 u64 limit1;
165 u64 limit2;
166 u64 cached_value;
167 unsigned long last_updated;
168 bool is_valid;
169 char const *acpi_name;
170};
171
172
173
174
175
176
177
178struct atk_acpi_ret_buffer {
179 u32 flags;
180 u32 value;
181 u8 data[];
182};
183
184
185struct atk_acpi_input_buf {
186 u32 id;
187 u32 param1;
188 u32 param2;
189};
190
191static int atk_add(struct acpi_device *device);
192static int atk_remove(struct acpi_device *device);
193static void atk_print_sensor(struct atk_data *data, union acpi_object *obj);
194static int atk_read_value(struct atk_sensor_data *sensor, u64 *value);
195static void atk_free_sensors(struct atk_data *data);
196
197static struct acpi_driver atk_driver = {
198 .name = ATK_HID,
199 .class = "hwmon",
200 .ids = atk_ids,
201 .ops = {
202 .add = atk_add,
203 .remove = atk_remove,
204 },
205};
206
207#define input_to_atk_sensor(attr) \
208 container_of(attr, struct atk_sensor_data, input_attr)
209
210#define label_to_atk_sensor(attr) \
211 container_of(attr, struct atk_sensor_data, label_attr)
212
213#define limit1_to_atk_sensor(attr) \
214 container_of(attr, struct atk_sensor_data, limit1_attr)
215
216#define limit2_to_atk_sensor(attr) \
217 container_of(attr, struct atk_sensor_data, limit2_attr)
218
219static ssize_t atk_input_show(struct device *dev,
220 struct device_attribute *attr, char *buf)
221{
222 struct atk_sensor_data *s = input_to_atk_sensor(attr);
223 u64 value;
224 int err;
225
226 err = atk_read_value(s, &value);
227 if (err)
228 return err;
229
230 if (s->type == HWMON_TYPE_TEMP)
231
232 value *= 100;
233
234 return sprintf(buf, "%llu\n", value);
235}
236
237static ssize_t atk_label_show(struct device *dev,
238 struct device_attribute *attr, char *buf)
239{
240 struct atk_sensor_data *s = label_to_atk_sensor(attr);
241
242 return sprintf(buf, "%s\n", s->acpi_name);
243}
244
245static ssize_t atk_limit1_show(struct device *dev,
246 struct device_attribute *attr, char *buf)
247{
248 struct atk_sensor_data *s = limit1_to_atk_sensor(attr);
249 u64 value = s->limit1;
250
251 if (s->type == HWMON_TYPE_TEMP)
252 value *= 100;
253
254 return sprintf(buf, "%lld\n", value);
255}
256
257static ssize_t atk_limit2_show(struct device *dev,
258 struct device_attribute *attr, char *buf)
259{
260 struct atk_sensor_data *s = limit2_to_atk_sensor(attr);
261 u64 value = s->limit2;
262
263 if (s->type == HWMON_TYPE_TEMP)
264 value *= 100;
265
266 return sprintf(buf, "%lld\n", value);
267}
268
269static ssize_t atk_name_show(struct device *dev,
270 struct device_attribute *attr, char *buf)
271{
272 return sprintf(buf, "atk0110\n");
273}
274static struct device_attribute atk_name_attr =
275 __ATTR(name, 0444, atk_name_show, NULL);
276
277static void atk_init_attribute(struct device_attribute *attr, char *name,
278 sysfs_show_func show)
279{
280 sysfs_attr_init(&attr->attr);
281 attr->attr.name = name;
282 attr->attr.mode = 0444;
283 attr->show = show;
284 attr->store = NULL;
285}
286
287
288static union acpi_object *atk_get_pack_member(struct atk_data *data,
289 union acpi_object *pack,
290 enum atk_pack_member m)
291{
292 bool old_if = data->old_interface;
293 int offset;
294
295 switch (m) {
296 case HWMON_PACK_FLAGS:
297 offset = old_if ? _HWMON_OLD_PACK_FLAGS : _HWMON_NEW_PACK_FLAGS;
298 break;
299 case HWMON_PACK_NAME:
300 offset = old_if ? _HWMON_OLD_PACK_NAME : _HWMON_NEW_PACK_NAME;
301 break;
302 case HWMON_PACK_LIMIT1:
303 offset = old_if ? _HWMON_OLD_PACK_LIMIT1 :
304 _HWMON_NEW_PACK_LIMIT1;
305 break;
306 case HWMON_PACK_LIMIT2:
307 offset = old_if ? _HWMON_OLD_PACK_LIMIT2 :
308 _HWMON_NEW_PACK_LIMIT2;
309 break;
310 case HWMON_PACK_ENABLE:
311 offset = old_if ? _HWMON_OLD_PACK_ENABLE :
312 _HWMON_NEW_PACK_ENABLE;
313 break;
314 default:
315 return NULL;
316 }
317
318 return &pack->package.elements[offset];
319}
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338static int validate_hwmon_pack(struct atk_data *data, union acpi_object *obj)
339{
340 struct device *dev = &data->acpi_dev->dev;
341 union acpi_object *tmp;
342 bool old_if = data->old_interface;
343 int const expected_size = old_if ? _HWMON_OLD_PACK_SIZE :
344 _HWMON_NEW_PACK_SIZE;
345
346 if (obj->type != ACPI_TYPE_PACKAGE) {
347 dev_warn(dev, "Invalid type: %d\n", obj->type);
348 return -EINVAL;
349 }
350
351 if (obj->package.count != expected_size) {
352 dev_warn(dev, "Invalid package size: %d, expected: %d\n",
353 obj->package.count, expected_size);
354 return -EINVAL;
355 }
356
357 tmp = atk_get_pack_member(data, obj, HWMON_PACK_FLAGS);
358 if (tmp->type != ACPI_TYPE_INTEGER) {
359 dev_warn(dev, "Invalid type (flag): %d\n", tmp->type);
360 return -EINVAL;
361 }
362
363 tmp = atk_get_pack_member(data, obj, HWMON_PACK_NAME);
364 if (tmp->type != ACPI_TYPE_STRING) {
365 dev_warn(dev, "Invalid type (name): %d\n", tmp->type);
366 return -EINVAL;
367 }
368
369
370#if 0
371 tmp = &obj->package.elements[HWMON_PACK_UNK1];
372 if (tmp->type != ACPI_TYPE_INTEGER) {
373 dev_warn(dev, "Invalid type (unk1): %d\n", tmp->type);
374 return -EINVAL;
375 }
376
377 tmp = &obj->package.elements[HWMON_PACK_UNK2];
378 if (tmp->type != ACPI_TYPE_INTEGER) {
379 dev_warn(dev, "Invalid type (unk2): %d\n", tmp->type);
380 return -EINVAL;
381 }
382#endif
383
384 tmp = atk_get_pack_member(data, obj, HWMON_PACK_LIMIT1);
385 if (tmp->type != ACPI_TYPE_INTEGER) {
386 dev_warn(dev, "Invalid type (limit1): %d\n", tmp->type);
387 return -EINVAL;
388 }
389
390 tmp = atk_get_pack_member(data, obj, HWMON_PACK_LIMIT2);
391 if (tmp->type != ACPI_TYPE_INTEGER) {
392 dev_warn(dev, "Invalid type (limit2): %d\n", tmp->type);
393 return -EINVAL;
394 }
395
396 tmp = atk_get_pack_member(data, obj, HWMON_PACK_ENABLE);
397 if (tmp->type != ACPI_TYPE_INTEGER) {
398 dev_warn(dev, "Invalid type (enable): %d\n", tmp->type);
399 return -EINVAL;
400 }
401
402 atk_print_sensor(data, obj);
403
404 return 0;
405}
406
407#ifdef DEBUG
408static char const *atk_sensor_type(union acpi_object *flags)
409{
410 u64 type = flags->integer.value & ATK_TYPE_MASK;
411 char const *what;
412
413 switch (type) {
414 case HWMON_TYPE_VOLT:
415 what = "voltage";
416 break;
417 case HWMON_TYPE_TEMP:
418 what = "temperature";
419 break;
420 case HWMON_TYPE_FAN:
421 what = "fan";
422 break;
423 default:
424 what = "unknown";
425 break;
426 }
427
428 return what;
429}
430#endif
431
432static void atk_print_sensor(struct atk_data *data, union acpi_object *obj)
433{
434#ifdef DEBUG
435 struct device *dev = &data->acpi_dev->dev;
436 union acpi_object *flags;
437 union acpi_object *name;
438 union acpi_object *limit1;
439 union acpi_object *limit2;
440 union acpi_object *enable;
441 char const *what;
442
443 flags = atk_get_pack_member(data, obj, HWMON_PACK_FLAGS);
444 name = atk_get_pack_member(data, obj, HWMON_PACK_NAME);
445 limit1 = atk_get_pack_member(data, obj, HWMON_PACK_LIMIT1);
446 limit2 = atk_get_pack_member(data, obj, HWMON_PACK_LIMIT2);
447 enable = atk_get_pack_member(data, obj, HWMON_PACK_ENABLE);
448
449 what = atk_sensor_type(flags);
450
451 dev_dbg(dev, "%s: %#llx %s [%llu-%llu] %s\n", what,
452 flags->integer.value,
453 name->string.pointer,
454 limit1->integer.value, limit2->integer.value,
455 enable->integer.value ? "enabled" : "disabled");
456#endif
457}
458
459static int atk_read_value_old(struct atk_sensor_data *sensor, u64 *value)
460{
461 struct atk_data *data = sensor->data;
462 struct device *dev = &data->acpi_dev->dev;
463 struct acpi_object_list params;
464 union acpi_object id;
465 acpi_status status;
466 acpi_handle method;
467
468 switch (sensor->type) {
469 case HWMON_TYPE_VOLT:
470 method = data->rvlt_handle;
471 break;
472 case HWMON_TYPE_TEMP:
473 method = data->rtmp_handle;
474 break;
475 case HWMON_TYPE_FAN:
476 method = data->rfan_handle;
477 break;
478 default:
479 return -EINVAL;
480 }
481
482 id.type = ACPI_TYPE_INTEGER;
483 id.integer.value = sensor->id;
484
485 params.count = 1;
486 params.pointer = &id;
487
488 status = acpi_evaluate_integer(method, NULL, ¶ms, value);
489 if (status != AE_OK) {
490 dev_warn(dev, "%s: ACPI exception: %s\n", __func__,
491 acpi_format_exception(status));
492 return -EIO;
493 }
494
495 return 0;
496}
497
498static union acpi_object *atk_ggrp(struct atk_data *data, u16 mux)
499{
500 struct device *dev = &data->acpi_dev->dev;
501 struct acpi_buffer buf;
502 acpi_status ret;
503 struct acpi_object_list params;
504 union acpi_object id;
505 union acpi_object *pack;
506
507 id.type = ACPI_TYPE_INTEGER;
508 id.integer.value = mux;
509 params.count = 1;
510 params.pointer = &id;
511
512 buf.length = ACPI_ALLOCATE_BUFFER;
513 ret = acpi_evaluate_object(data->enumerate_handle, NULL, ¶ms, &buf);
514 if (ret != AE_OK) {
515 dev_err(dev, "GGRP[%#x] ACPI exception: %s\n", mux,
516 acpi_format_exception(ret));
517 return ERR_PTR(-EIO);
518 }
519 pack = buf.pointer;
520 if (pack->type != ACPI_TYPE_PACKAGE) {
521
522 ACPI_FREE(pack);
523 return ERR_PTR(-ENOENT);
524 }
525
526 if (pack->package.count < 1) {
527 dev_err(dev, "GGRP[%#x] package is too small\n", mux);
528 ACPI_FREE(pack);
529 return ERR_PTR(-EIO);
530 }
531 return pack;
532}
533
534static union acpi_object *atk_gitm(struct atk_data *data, u64 id)
535{
536 struct device *dev = &data->acpi_dev->dev;
537 struct atk_acpi_input_buf buf;
538 union acpi_object tmp;
539 struct acpi_object_list params;
540 struct acpi_buffer ret;
541 union acpi_object *obj;
542 acpi_status status;
543
544 buf.id = id;
545 buf.param1 = 0;
546 buf.param2 = 0;
547
548 tmp.type = ACPI_TYPE_BUFFER;
549 tmp.buffer.pointer = (u8 *)&buf;
550 tmp.buffer.length = sizeof(buf);
551
552 params.count = 1;
553 params.pointer = (void *)&tmp;
554
555 ret.length = ACPI_ALLOCATE_BUFFER;
556 status = acpi_evaluate_object_typed(data->read_handle, NULL, ¶ms,
557 &ret, ACPI_TYPE_BUFFER);
558 if (status != AE_OK) {
559 dev_warn(dev, "GITM[%#llx] ACPI exception: %s\n", id,
560 acpi_format_exception(status));
561 return ERR_PTR(-EIO);
562 }
563 obj = ret.pointer;
564
565
566 if (obj->buffer.length < 8) {
567 dev_warn(dev, "Unexpected ASBF length: %u\n",
568 obj->buffer.length);
569 ACPI_FREE(obj);
570 return ERR_PTR(-EIO);
571 }
572 return obj;
573}
574
575static union acpi_object *atk_sitm(struct atk_data *data,
576 struct atk_acpi_input_buf *buf)
577{
578 struct device *dev = &data->acpi_dev->dev;
579 struct acpi_object_list params;
580 union acpi_object tmp;
581 struct acpi_buffer ret;
582 union acpi_object *obj;
583 acpi_status status;
584
585 tmp.type = ACPI_TYPE_BUFFER;
586 tmp.buffer.pointer = (u8 *)buf;
587 tmp.buffer.length = sizeof(*buf);
588
589 params.count = 1;
590 params.pointer = &tmp;
591
592 ret.length = ACPI_ALLOCATE_BUFFER;
593 status = acpi_evaluate_object_typed(data->write_handle, NULL, ¶ms,
594 &ret, ACPI_TYPE_BUFFER);
595 if (status != AE_OK) {
596 dev_warn(dev, "SITM[%#x] ACPI exception: %s\n", buf->id,
597 acpi_format_exception(status));
598 return ERR_PTR(-EIO);
599 }
600 obj = ret.pointer;
601
602
603 if (obj->buffer.length < 8) {
604 dev_warn(dev, "Unexpected ASBF length: %u\n",
605 obj->buffer.length);
606 ACPI_FREE(obj);
607 return ERR_PTR(-EIO);
608 }
609 return obj;
610}
611
612static int atk_read_value_new(struct atk_sensor_data *sensor, u64 *value)
613{
614 struct atk_data *data = sensor->data;
615 struct device *dev = &data->acpi_dev->dev;
616 union acpi_object *obj;
617 struct atk_acpi_ret_buffer *buf;
618 int err = 0;
619
620 obj = atk_gitm(data, sensor->id);
621 if (IS_ERR(obj))
622 return PTR_ERR(obj);
623
624 buf = (struct atk_acpi_ret_buffer *)obj->buffer.pointer;
625 if (buf->flags == 0) {
626
627
628
629
630
631 dev_warn(dev, "Read failed, sensor = %#llx\n", sensor->id);
632 err = -EIO;
633 goto out;
634 }
635
636 *value = buf->value;
637out:
638 ACPI_FREE(obj);
639 return err;
640}
641
642static int atk_read_value(struct atk_sensor_data *sensor, u64 *value)
643{
644 int err;
645
646 if (!sensor->is_valid ||
647 time_after(jiffies, sensor->last_updated + CACHE_TIME)) {
648 if (sensor->data->old_interface)
649 err = atk_read_value_old(sensor, value);
650 else
651 err = atk_read_value_new(sensor, value);
652
653 sensor->is_valid = true;
654 sensor->last_updated = jiffies;
655 sensor->cached_value = *value;
656 } else {
657 *value = sensor->cached_value;
658 err = 0;
659 }
660
661 return err;
662}
663
664#ifdef CONFIG_DEBUG_FS
665static int atk_debugfs_gitm_get(void *p, u64 *val)
666{
667 struct atk_data *data = p;
668 union acpi_object *ret;
669 struct atk_acpi_ret_buffer *buf;
670 int err = 0;
671
672 if (!data->read_handle)
673 return -ENODEV;
674
675 if (!data->debugfs.id)
676 return -EINVAL;
677
678 ret = atk_gitm(data, data->debugfs.id);
679 if (IS_ERR(ret))
680 return PTR_ERR(ret);
681
682 buf = (struct atk_acpi_ret_buffer *)ret->buffer.pointer;
683 if (buf->flags)
684 *val = buf->value;
685 else
686 err = -EIO;
687
688 ACPI_FREE(ret);
689 return err;
690}
691
692DEFINE_SIMPLE_ATTRIBUTE(atk_debugfs_gitm,
693 atk_debugfs_gitm_get,
694 NULL,
695 "0x%08llx\n")
696
697static int atk_acpi_print(char *buf, size_t sz, union acpi_object *obj)
698{
699 int ret = 0;
700
701 switch (obj->type) {
702 case ACPI_TYPE_INTEGER:
703 ret = snprintf(buf, sz, "0x%08llx\n", obj->integer.value);
704 break;
705 case ACPI_TYPE_STRING:
706 ret = snprintf(buf, sz, "%s\n", obj->string.pointer);
707 break;
708 }
709
710 return ret;
711}
712
713static void atk_pack_print(char *buf, size_t sz, union acpi_object *pack)
714{
715 int ret;
716 int i;
717
718 for (i = 0; i < pack->package.count; i++) {
719 union acpi_object *obj = &pack->package.elements[i];
720
721 ret = atk_acpi_print(buf, sz, obj);
722 if (ret >= sz)
723 break;
724 buf += ret;
725 sz -= ret;
726 }
727}
728
729static int atk_debugfs_ggrp_open(struct inode *inode, struct file *file)
730{
731 struct atk_data *data = inode->i_private;
732 char *buf = NULL;
733 union acpi_object *ret;
734 u8 cls;
735 int i;
736
737 if (!data->enumerate_handle)
738 return -ENODEV;
739 if (!data->debugfs.id)
740 return -EINVAL;
741
742 cls = (data->debugfs.id & 0xff000000) >> 24;
743 ret = atk_ggrp(data, cls);
744 if (IS_ERR(ret))
745 return PTR_ERR(ret);
746
747 for (i = 0; i < ret->package.count; i++) {
748 union acpi_object *pack = &ret->package.elements[i];
749 union acpi_object *id;
750
751 if (pack->type != ACPI_TYPE_PACKAGE)
752 continue;
753 if (!pack->package.count)
754 continue;
755 id = &pack->package.elements[0];
756 if (id->integer.value == data->debugfs.id) {
757
758 buf = kzalloc(512, GFP_KERNEL);
759 if (!buf) {
760 ACPI_FREE(ret);
761 return -ENOMEM;
762 }
763 atk_pack_print(buf, 512, pack);
764 break;
765 }
766 }
767 ACPI_FREE(ret);
768
769 if (!buf)
770 return -EINVAL;
771
772 file->private_data = buf;
773
774 return nonseekable_open(inode, file);
775}
776
777static ssize_t atk_debugfs_ggrp_read(struct file *file, char __user *buf,
778 size_t count, loff_t *pos)
779{
780 char *str = file->private_data;
781 size_t len = strlen(str);
782
783 return simple_read_from_buffer(buf, count, pos, str, len);
784}
785
786static int atk_debugfs_ggrp_release(struct inode *inode, struct file *file)
787{
788 kfree(file->private_data);
789 return 0;
790}
791
792static const struct file_operations atk_debugfs_ggrp_fops = {
793 .read = atk_debugfs_ggrp_read,
794 .open = atk_debugfs_ggrp_open,
795 .release = atk_debugfs_ggrp_release,
796 .llseek = no_llseek,
797};
798
799static void atk_debugfs_init(struct atk_data *data)
800{
801 struct dentry *d;
802 struct dentry *f;
803
804 data->debugfs.id = 0;
805
806 d = debugfs_create_dir("asus_atk0110", NULL);
807 if (!d || IS_ERR(d))
808 return;
809
810 f = debugfs_create_x32("id", S_IRUSR | S_IWUSR, d, &data->debugfs.id);
811 if (!f || IS_ERR(f))
812 goto cleanup;
813
814 f = debugfs_create_file("gitm", S_IRUSR, d, data,
815 &atk_debugfs_gitm);
816 if (!f || IS_ERR(f))
817 goto cleanup;
818
819 f = debugfs_create_file("ggrp", S_IRUSR, d, data,
820 &atk_debugfs_ggrp_fops);
821 if (!f || IS_ERR(f))
822 goto cleanup;
823
824 data->debugfs.root = d;
825
826 return;
827cleanup:
828 debugfs_remove_recursive(d);
829}
830
831static void atk_debugfs_cleanup(struct atk_data *data)
832{
833 debugfs_remove_recursive(data->debugfs.root);
834}
835
836#else
837
838static void atk_debugfs_init(struct atk_data *data)
839{
840}
841
842static void atk_debugfs_cleanup(struct atk_data *data)
843{
844}
845#endif
846
847static int atk_add_sensor(struct atk_data *data, union acpi_object *obj)
848{
849 struct device *dev = &data->acpi_dev->dev;
850 union acpi_object *flags;
851 union acpi_object *name;
852 union acpi_object *limit1;
853 union acpi_object *limit2;
854 union acpi_object *enable;
855 struct atk_sensor_data *sensor;
856 char const *base_name;
857 char const *limit1_name;
858 char const *limit2_name;
859 u64 type;
860 int err;
861 int *num;
862 int start;
863
864 if (obj->type != ACPI_TYPE_PACKAGE) {
865
866 dev_warn(dev, "Unknown type for ACPI object: (%d)\n",
867 obj->type);
868 return -EINVAL;
869 }
870
871 err = validate_hwmon_pack(data, obj);
872 if (err)
873 return err;
874
875
876 type = atk_get_pack_member(data, obj, HWMON_PACK_FLAGS)->integer.value
877 & ATK_TYPE_MASK;
878
879 switch (type) {
880 case HWMON_TYPE_VOLT:
881 base_name = "in";
882 limit1_name = "min";
883 limit2_name = "max";
884 num = &data->voltage_count;
885 start = 0;
886 break;
887 case HWMON_TYPE_TEMP:
888 base_name = "temp";
889 limit1_name = "max";
890 limit2_name = "crit";
891 num = &data->temperature_count;
892 start = 1;
893 break;
894 case HWMON_TYPE_FAN:
895 base_name = "fan";
896 limit1_name = "min";
897 limit2_name = "max";
898 num = &data->fan_count;
899 start = 1;
900 break;
901 default:
902 dev_warn(dev, "Unknown sensor type: %#llx\n", type);
903 return -EINVAL;
904 }
905
906 enable = atk_get_pack_member(data, obj, HWMON_PACK_ENABLE);
907 if (!enable->integer.value)
908
909 return 0;
910
911 flags = atk_get_pack_member(data, obj, HWMON_PACK_FLAGS);
912 name = atk_get_pack_member(data, obj, HWMON_PACK_NAME);
913 limit1 = atk_get_pack_member(data, obj, HWMON_PACK_LIMIT1);
914 limit2 = atk_get_pack_member(data, obj, HWMON_PACK_LIMIT2);
915
916 sensor = kzalloc(sizeof(*sensor), GFP_KERNEL);
917 if (!sensor)
918 return -ENOMEM;
919
920 sensor->acpi_name = kstrdup(name->string.pointer, GFP_KERNEL);
921 if (!sensor->acpi_name) {
922 err = -ENOMEM;
923 goto out;
924 }
925
926 INIT_LIST_HEAD(&sensor->list);
927 sensor->type = type;
928 sensor->data = data;
929 sensor->id = flags->integer.value;
930 sensor->limit1 = limit1->integer.value;
931 if (data->old_interface)
932 sensor->limit2 = limit2->integer.value;
933 else
934
935 sensor->limit2 = sensor->limit1 + limit2->integer.value;
936
937 snprintf(sensor->input_attr_name, ATTR_NAME_SIZE,
938 "%s%d_input", base_name, start + *num);
939 atk_init_attribute(&sensor->input_attr,
940 sensor->input_attr_name,
941 atk_input_show);
942
943 snprintf(sensor->label_attr_name, ATTR_NAME_SIZE,
944 "%s%d_label", base_name, start + *num);
945 atk_init_attribute(&sensor->label_attr,
946 sensor->label_attr_name,
947 atk_label_show);
948
949 snprintf(sensor->limit1_attr_name, ATTR_NAME_SIZE,
950 "%s%d_%s", base_name, start + *num, limit1_name);
951 atk_init_attribute(&sensor->limit1_attr,
952 sensor->limit1_attr_name,
953 atk_limit1_show);
954
955 snprintf(sensor->limit2_attr_name, ATTR_NAME_SIZE,
956 "%s%d_%s", base_name, start + *num, limit2_name);
957 atk_init_attribute(&sensor->limit2_attr,
958 sensor->limit2_attr_name,
959 atk_limit2_show);
960
961 list_add(&sensor->list, &data->sensor_list);
962 (*num)++;
963
964 return 1;
965out:
966 kfree(sensor);
967 return err;
968}
969
970static int atk_enumerate_old_hwmon(struct atk_data *data)
971{
972 struct device *dev = &data->acpi_dev->dev;
973 struct acpi_buffer buf;
974 union acpi_object *pack;
975 acpi_status status;
976 int i, ret;
977 int count = 0;
978
979
980 buf.length = ACPI_ALLOCATE_BUFFER;
981 status = acpi_evaluate_object_typed(data->atk_handle,
982 METHOD_OLD_ENUM_VLT, NULL, &buf, ACPI_TYPE_PACKAGE);
983 if (status != AE_OK) {
984 dev_warn(dev, METHOD_OLD_ENUM_VLT ": ACPI exception: %s\n",
985 acpi_format_exception(status));
986
987 return -ENODEV;
988 }
989
990 pack = buf.pointer;
991 for (i = 1; i < pack->package.count; i++) {
992 union acpi_object *obj = &pack->package.elements[i];
993
994 ret = atk_add_sensor(data, obj);
995 if (ret > 0)
996 count++;
997 }
998 ACPI_FREE(buf.pointer);
999
1000
1001 buf.length = ACPI_ALLOCATE_BUFFER;
1002 status = acpi_evaluate_object_typed(data->atk_handle,
1003 METHOD_OLD_ENUM_TMP, NULL, &buf, ACPI_TYPE_PACKAGE);
1004 if (status != AE_OK) {
1005 dev_warn(dev, METHOD_OLD_ENUM_TMP ": ACPI exception: %s\n",
1006 acpi_format_exception(status));
1007
1008 ret = -ENODEV;
1009 goto cleanup;
1010 }
1011
1012 pack = buf.pointer;
1013 for (i = 1; i < pack->package.count; i++) {
1014 union acpi_object *obj = &pack->package.elements[i];
1015
1016 ret = atk_add_sensor(data, obj);
1017 if (ret > 0)
1018 count++;
1019 }
1020 ACPI_FREE(buf.pointer);
1021
1022
1023 buf.length = ACPI_ALLOCATE_BUFFER;
1024 status = acpi_evaluate_object_typed(data->atk_handle,
1025 METHOD_OLD_ENUM_FAN, NULL, &buf, ACPI_TYPE_PACKAGE);
1026 if (status != AE_OK) {
1027 dev_warn(dev, METHOD_OLD_ENUM_FAN ": ACPI exception: %s\n",
1028 acpi_format_exception(status));
1029
1030 ret = -ENODEV;
1031 goto cleanup;
1032 }
1033
1034 pack = buf.pointer;
1035 for (i = 1; i < pack->package.count; i++) {
1036 union acpi_object *obj = &pack->package.elements[i];
1037
1038 ret = atk_add_sensor(data, obj);
1039 if (ret > 0)
1040 count++;
1041 }
1042 ACPI_FREE(buf.pointer);
1043
1044 return count;
1045cleanup:
1046 atk_free_sensors(data);
1047 return ret;
1048}
1049
1050static int atk_ec_present(struct atk_data *data)
1051{
1052 struct device *dev = &data->acpi_dev->dev;
1053 union acpi_object *pack;
1054 union acpi_object *ec;
1055 int ret;
1056 int i;
1057
1058 pack = atk_ggrp(data, ATK_MUX_MGMT);
1059 if (IS_ERR(pack)) {
1060 if (PTR_ERR(pack) == -ENOENT) {
1061
1062 dev_dbg(dev, "Class %#llx not found\n", ATK_MUX_MGMT);
1063 return 0;
1064 }
1065 return PTR_ERR(pack);
1066 }
1067
1068
1069 ec = NULL;
1070 for (i = 0; i < pack->package.count; i++) {
1071 union acpi_object *obj = &pack->package.elements[i];
1072 union acpi_object *id;
1073
1074 if (obj->type != ACPI_TYPE_PACKAGE)
1075 continue;
1076
1077 id = &obj->package.elements[0];
1078 if (id->type != ACPI_TYPE_INTEGER)
1079 continue;
1080
1081 if (id->integer.value == ATK_EC_ID) {
1082 ec = obj;
1083 break;
1084 }
1085 }
1086
1087 ret = (ec != NULL);
1088 if (!ret)
1089
1090 dev_dbg(dev, "EC not found\n");
1091
1092 ACPI_FREE(pack);
1093 return ret;
1094}
1095
1096static int atk_ec_enabled(struct atk_data *data)
1097{
1098 struct device *dev = &data->acpi_dev->dev;
1099 union acpi_object *obj;
1100 struct atk_acpi_ret_buffer *buf;
1101 int err;
1102
1103 obj = atk_gitm(data, ATK_EC_ID);
1104 if (IS_ERR(obj)) {
1105 dev_err(dev, "Unable to query EC status\n");
1106 return PTR_ERR(obj);
1107 }
1108 buf = (struct atk_acpi_ret_buffer *)obj->buffer.pointer;
1109
1110 if (buf->flags == 0) {
1111 dev_err(dev, "Unable to query EC status\n");
1112 err = -EIO;
1113 } else {
1114 err = (buf->value != 0);
1115 dev_dbg(dev, "EC is %sabled\n",
1116 err ? "en" : "dis");
1117 }
1118
1119 ACPI_FREE(obj);
1120 return err;
1121}
1122
1123static int atk_ec_ctl(struct atk_data *data, int enable)
1124{
1125 struct device *dev = &data->acpi_dev->dev;
1126 union acpi_object *obj;
1127 struct atk_acpi_input_buf sitm;
1128 struct atk_acpi_ret_buffer *ec_ret;
1129 int err = 0;
1130
1131 sitm.id = ATK_EC_ID;
1132 sitm.param1 = enable;
1133 sitm.param2 = 0;
1134
1135 obj = atk_sitm(data, &sitm);
1136 if (IS_ERR(obj)) {
1137 dev_err(dev, "Failed to %sable the EC\n",
1138 enable ? "en" : "dis");
1139 return PTR_ERR(obj);
1140 }
1141 ec_ret = (struct atk_acpi_ret_buffer *)obj->buffer.pointer;
1142 if (ec_ret->flags == 0) {
1143 dev_err(dev, "Failed to %sable the EC\n",
1144 enable ? "en" : "dis");
1145 err = -EIO;
1146 } else {
1147 dev_info(dev, "EC %sabled\n",
1148 enable ? "en" : "dis");
1149 }
1150
1151 ACPI_FREE(obj);
1152 return err;
1153}
1154
1155static int atk_enumerate_new_hwmon(struct atk_data *data)
1156{
1157 struct device *dev = &data->acpi_dev->dev;
1158 union acpi_object *pack;
1159 int err;
1160 int i;
1161
1162 err = atk_ec_present(data);
1163 if (err < 0)
1164 return err;
1165 if (err) {
1166 err = atk_ec_enabled(data);
1167 if (err < 0)
1168 return err;
1169
1170 data->disable_ec = err;
1171
1172 err = atk_ec_ctl(data, 1);
1173 if (err) {
1174 data->disable_ec = false;
1175 return err;
1176 }
1177 }
1178
1179 dev_dbg(dev, "Enumerating hwmon sensors\n");
1180
1181 pack = atk_ggrp(data, ATK_MUX_HWMON);
1182 if (IS_ERR(pack))
1183 return PTR_ERR(pack);
1184
1185 for (i = 0; i < pack->package.count; i++) {
1186 union acpi_object *obj = &pack->package.elements[i];
1187
1188 atk_add_sensor(data, obj);
1189 }
1190
1191 err = data->voltage_count + data->temperature_count + data->fan_count;
1192
1193 ACPI_FREE(pack);
1194 return err;
1195}
1196
1197static int atk_create_files(struct atk_data *data)
1198{
1199 struct atk_sensor_data *s;
1200 int err;
1201
1202 list_for_each_entry(s, &data->sensor_list, list) {
1203 err = device_create_file(data->hwmon_dev, &s->input_attr);
1204 if (err)
1205 return err;
1206 err = device_create_file(data->hwmon_dev, &s->label_attr);
1207 if (err)
1208 return err;
1209 err = device_create_file(data->hwmon_dev, &s->limit1_attr);
1210 if (err)
1211 return err;
1212 err = device_create_file(data->hwmon_dev, &s->limit2_attr);
1213 if (err)
1214 return err;
1215 }
1216
1217 err = device_create_file(data->hwmon_dev, &atk_name_attr);
1218
1219 return err;
1220}
1221
1222static void atk_remove_files(struct atk_data *data)
1223{
1224 struct atk_sensor_data *s;
1225
1226 list_for_each_entry(s, &data->sensor_list, list) {
1227 device_remove_file(data->hwmon_dev, &s->input_attr);
1228 device_remove_file(data->hwmon_dev, &s->label_attr);
1229 device_remove_file(data->hwmon_dev, &s->limit1_attr);
1230 device_remove_file(data->hwmon_dev, &s->limit2_attr);
1231 }
1232 device_remove_file(data->hwmon_dev, &atk_name_attr);
1233}
1234
1235static void atk_free_sensors(struct atk_data *data)
1236{
1237 struct list_head *head = &data->sensor_list;
1238 struct atk_sensor_data *s, *tmp;
1239
1240 list_for_each_entry_safe(s, tmp, head, list) {
1241 kfree(s->acpi_name);
1242 kfree(s);
1243 }
1244}
1245
1246static int atk_register_hwmon(struct atk_data *data)
1247{
1248 struct device *dev = &data->acpi_dev->dev;
1249 int err;
1250
1251 dev_dbg(dev, "registering hwmon device\n");
1252 data->hwmon_dev = hwmon_device_register(dev);
1253 if (IS_ERR(data->hwmon_dev))
1254 return PTR_ERR(data->hwmon_dev);
1255
1256 dev_dbg(dev, "populating sysfs directory\n");
1257 err = atk_create_files(data);
1258 if (err)
1259 goto remove;
1260
1261 return 0;
1262remove:
1263
1264 atk_remove_files(data);
1265 hwmon_device_unregister(data->hwmon_dev);
1266 return err;
1267}
1268
1269static int atk_probe_if(struct atk_data *data)
1270{
1271 struct device *dev = &data->acpi_dev->dev;
1272 acpi_handle ret;
1273 acpi_status status;
1274 int err = 0;
1275
1276
1277 status = acpi_get_handle(data->atk_handle, METHOD_OLD_READ_TMP, &ret);
1278 if (ACPI_SUCCESS(status))
1279 data->rtmp_handle = ret;
1280 else
1281 dev_dbg(dev, "method " METHOD_OLD_READ_TMP " not found: %s\n",
1282 acpi_format_exception(status));
1283
1284
1285 status = acpi_get_handle(data->atk_handle, METHOD_OLD_READ_VLT, &ret);
1286 if (ACPI_SUCCESS(status))
1287 data->rvlt_handle = ret;
1288 else
1289 dev_dbg(dev, "method " METHOD_OLD_READ_VLT " not found: %s\n",
1290 acpi_format_exception(status));
1291
1292
1293 status = acpi_get_handle(data->atk_handle, METHOD_OLD_READ_FAN, &ret);
1294 if (ACPI_SUCCESS(status))
1295 data->rfan_handle = ret;
1296 else
1297 dev_dbg(dev, "method " METHOD_OLD_READ_FAN " not found: %s\n",
1298 acpi_format_exception(status));
1299
1300
1301 status = acpi_get_handle(data->atk_handle, METHOD_ENUMERATE, &ret);
1302 if (ACPI_SUCCESS(status))
1303 data->enumerate_handle = ret;
1304 else
1305 dev_dbg(dev, "method " METHOD_ENUMERATE " not found: %s\n",
1306 acpi_format_exception(status));
1307
1308
1309 status = acpi_get_handle(data->atk_handle, METHOD_READ, &ret);
1310 if (ACPI_SUCCESS(status))
1311 data->read_handle = ret;
1312 else
1313 dev_dbg(dev, "method " METHOD_READ " not found: %s\n",
1314 acpi_format_exception(status));
1315
1316
1317 status = acpi_get_handle(data->atk_handle, METHOD_WRITE, &ret);
1318 if (ACPI_SUCCESS(status))
1319 data->write_handle = ret;
1320 else
1321 dev_dbg(dev, "method " METHOD_WRITE " not found: %s\n",
1322 acpi_format_exception(status));
1323
1324
1325
1326
1327
1328
1329
1330 if (new_if)
1331 dev_info(dev, "Overriding interface detection\n");
1332 if (data->rtmp_handle &&
1333 data->rvlt_handle && data->rfan_handle && !new_if)
1334 data->old_interface = true;
1335 else if (data->enumerate_handle && data->read_handle &&
1336 data->write_handle)
1337 data->old_interface = false;
1338 else
1339 err = -ENODEV;
1340
1341 return err;
1342}
1343
1344static int atk_add(struct acpi_device *device)
1345{
1346 acpi_status ret;
1347 int err;
1348 struct acpi_buffer buf;
1349 union acpi_object *obj;
1350 struct atk_data *data;
1351
1352 dev_dbg(&device->dev, "adding...\n");
1353
1354 data = kzalloc(sizeof(*data), GFP_KERNEL);
1355 if (!data)
1356 return -ENOMEM;
1357
1358 data->acpi_dev = device;
1359 data->atk_handle = device->handle;
1360 INIT_LIST_HEAD(&data->sensor_list);
1361 data->disable_ec = false;
1362
1363 buf.length = ACPI_ALLOCATE_BUFFER;
1364 ret = acpi_evaluate_object_typed(data->atk_handle, BOARD_ID, NULL,
1365 &buf, ACPI_TYPE_PACKAGE);
1366 if (ret != AE_OK) {
1367 dev_dbg(&device->dev, "atk: method MBIF not found\n");
1368 } else {
1369 obj = buf.pointer;
1370 if (obj->package.count >= 2) {
1371 union acpi_object *id = &obj->package.elements[1];
1372 if (id->type == ACPI_TYPE_STRING)
1373 dev_dbg(&device->dev, "board ID = %s\n",
1374 id->string.pointer);
1375 }
1376 ACPI_FREE(buf.pointer);
1377 }
1378
1379 err = atk_probe_if(data);
1380 if (err) {
1381 dev_err(&device->dev, "No usable hwmon interface detected\n");
1382 goto out;
1383 }
1384
1385 if (data->old_interface) {
1386 dev_dbg(&device->dev, "Using old hwmon interface\n");
1387 err = atk_enumerate_old_hwmon(data);
1388 } else {
1389 dev_dbg(&device->dev, "Using new hwmon interface\n");
1390 err = atk_enumerate_new_hwmon(data);
1391 }
1392 if (err < 0)
1393 goto out;
1394 if (err == 0) {
1395 dev_info(&device->dev,
1396 "No usable sensor detected, bailing out\n");
1397 err = -ENODEV;
1398 goto out;
1399 }
1400
1401 err = atk_register_hwmon(data);
1402 if (err)
1403 goto cleanup;
1404
1405 atk_debugfs_init(data);
1406
1407 device->driver_data = data;
1408 return 0;
1409cleanup:
1410 atk_free_sensors(data);
1411out:
1412 if (data->disable_ec)
1413 atk_ec_ctl(data, 0);
1414 kfree(data);
1415 return err;
1416}
1417
1418static int atk_remove(struct acpi_device *device)
1419{
1420 struct atk_data *data = device->driver_data;
1421 dev_dbg(&device->dev, "removing...\n");
1422
1423 device->driver_data = NULL;
1424
1425 atk_debugfs_cleanup(data);
1426
1427 atk_remove_files(data);
1428 atk_free_sensors(data);
1429 hwmon_device_unregister(data->hwmon_dev);
1430
1431 if (data->disable_ec) {
1432 if (atk_ec_ctl(data, 0))
1433 dev_err(&device->dev, "Failed to disable EC\n");
1434 }
1435
1436 kfree(data);
1437
1438 return 0;
1439}
1440
1441static int __init atk0110_init(void)
1442{
1443 int ret;
1444
1445
1446 if (!acpi_resources_are_enforced()) {
1447 pr_err("Resources not safely usable due to acpi_enforce_resources kernel parameter\n");
1448 return -EBUSY;
1449 }
1450
1451 if (dmi_check_system(atk_force_new_if))
1452 new_if = true;
1453
1454 ret = acpi_bus_register_driver(&atk_driver);
1455 if (ret)
1456 pr_info("acpi_bus_register_driver failed: %d\n", ret);
1457
1458 return ret;
1459}
1460
1461static void __exit atk0110_exit(void)
1462{
1463 acpi_bus_unregister_driver(&atk_driver);
1464}
1465
1466module_init(atk0110_init);
1467module_exit(atk0110_exit);
1468
1469MODULE_LICENSE("GPL");
1470