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