removed useless whitespaces
[psensor.git] / src / lib / amd.c
index e7befe5..e702e1a 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * Copyright (C) 2010-2011 thgreasi@gmail.com, jeanfi@gmail.com
- * Copyright (C) 2010-2012 jeanfi@gmail.com
+ * Copyright (C) 2010-2013 jeanfi@gmail.com
  *
  * This program is free software; you can redistribute it and/or
  * modify it under the terms of the GNU General Public License as
@@ -56,16 +56,15 @@ static ADL_OVERDRIVE5_FANSPEED_GET        adl_overdrive5_fanspeed_get;
 
 static void *hdll;
 static int adl_main_control_done;
-static int *active_amd_adapters;
+static int *active_adapters;
 
-/* Memory allocation function */
 static void __stdcall *adl_main_memory_alloc(int isize)
 {
        void *lpbuffer = malloc(isize);
        return lpbuffer;
 }
 
-static void *getprocaddress(void *plibrary, const char * name)
+static void *getprocaddress(void *plibrary, const char *name)
 {
        return dlsym(plibrary, name);
 }
@@ -111,20 +110,24 @@ static struct psensor *create_sensor(int id, int values_len)
        if (id & 1) {/* odd number ids represent fan sensors */
                id = id >> 1;
                sprintf(name, "GPU%dfan", id);
-               sensor_type = SENSOR_TYPE_AMD_FAN;
+               sensor_type = SENSOR_TYPE_ATIADL
+                       | SENSOR_TYPE_FAN
+                       | SENSOR_TYPE_RPM;
        } else {/* even number ids represent temperature sensors */
                id = id >> 1;
                sprintf(name, "GPU%dtemp", id);
-               sensor_type = SENSOR_TYPE_AMD_TEMP;
+               sensor_type = SENSOR_TYPE_ATIADL
+                       | SENSOR_TYPE_GPU
+                       | SENSOR_TYPE_TEMP;
        }
 
        sid = malloc(strlen("amd") + 1 + strlen(name) + 1);
        sprintf(sid, "amd %s", name);
 
-       s = psensor_create(sid, strdup(name),
+       s = psensor_create(sid, strdup(name), strdup("ATI GPU"),
                           sensor_type, values_len);
 
-       s->amd_id = active_amd_adapters[id];
+       s->amd_id = active_adapters[id];
 
        return s;
 }
@@ -141,14 +144,12 @@ static int init()
        int i, inumberadapters, inumberadaptersactive = 0;
        int lpstatus, iadapterindex;
 
-       hdll = NULL;
        adl_main_control_done = 0;
-       active_amd_adapters = NULL;
+       active_adapters = NULL;
        hdll = dlopen("libatiadlxx.so", RTLD_LAZY|RTLD_GLOBAL);
 
        if (!hdll) {
-               fprintf(stderr,
-                       _("ERROR: ADL library not found!\n"));
+               log_err(_("AMD: cannot found ADL library."));
                return 0;
        }
 
@@ -172,25 +173,18 @@ static int init()
                !adl_adapter_adapterinfo_get ||
                !adl_overdrive5_temperature_get ||
                !adl_overdrive5_fanspeed_get) {
-               fprintf(stderr,
-                       _("ERROR: ADL's API is missing!\n"));
+               log_err(_("AMD: missing ADL's API."));
                return 0;
        }
 
-       /* Initialize ADL. The second parameter is 1, which means:
-          retrieve adapter information only for adapters that
-          are physically present and enabled in the system */
        if (ADL_OK != adl_main_control_create(adl_main_memory_alloc, 1)) {
-               fprintf(stderr,
-                       _("ERROR: ADL Initialization Error!\n"));
+               log_err(_("AMD: failed to initialize ADL."));
                return 0;
        }
        adl_main_control_done = 1;
 
-       /* Obtain the number of adapters for the system */
        if (ADL_OK != adl_adapter_numberofadapters_get(&inumberadapters)) {
-               fprintf(stderr,
-                       _("ERROR: Cannot get the number of adapters!\n"));
+               log_err(_("AMD: cannot get the number of adapters."));
                return 0;
        }
 
@@ -200,11 +194,9 @@ static int init()
        lpadapterinfo = malloc(sizeof(AdapterInfo) * inumberadapters);
        memset(lpadapterinfo, '\0', sizeof(AdapterInfo) * inumberadapters);
 
-       /* Get the AdapterInfo structure for all adapters in the system */
        adl_adapter_adapterinfo_get(lpadapterinfo,
                                    sizeof(AdapterInfo) * inumberadapters);
 
-       /* Repeat for all available adapters in the system */
        for (i = 0; i < inumberadapters; i++) {
 
                iadapterindex = lpadapterinfo[i].iAdapterIndex;
@@ -212,25 +204,26 @@ static int init()
                if (ADL_OK != adl_adapter_active_get(iadapterindex, &lpstatus))
                        continue;
                if (lpstatus != ADL_TRUE)
-                       /* count only if the adapter is active */
                        continue;
 
-               if (!active_amd_adapters) {
-                       active_amd_adapters = (int *) malloc(sizeof(int));
+               if (!active_adapters) {
+                       active_adapters = (int *) malloc(sizeof(int));
                        inumberadaptersactive = 1;
                } else {
                        ++inumberadaptersactive;
-                       active_amd_adapters = (int *)realloc
-                               (active_amd_adapters,
+                       active_adapters = (int *)realloc
+                               (active_adapters,
                                 sizeof(int)*inumberadaptersactive);
+
+                       if (!active_adapters)
+                               exit(EXIT_FAILURE);
                }
-               active_amd_adapters[inumberadaptersactive-1] = iadapterindex;
+               active_adapters[inumberadaptersactive-1] = iadapterindex;
        }
 
        free(lpadapterinfo);
 
-       /* Each Adapter has one GPU temperature sensor and one fan
-          control sensor */
+       /* Each Adapter has one temperature sensor and one fan */
        return 2*inumberadaptersactive;
 }
 
@@ -242,12 +235,14 @@ void amd_psensor_list_update(struct psensor **sensors)
        while (*ss) {
                s = *ss;
 
-               if (s->type == SENSOR_TYPE_AMD_TEMP)
-                       psensor_set_current_value(s, get_temp(s));
-               else if (s->type == SENSOR_TYPE_AMD_FAN)
-                       psensor_set_current_value(s, get_fanspeed(s));
-
-               ss++;
+               if (s->type & SENSOR_TYPE_ATIADL) {
+                       if (s->type & SENSOR_TYPE_TEMP)
+                               psensor_set_current_value(s, get_temp(s));
+                       else if (s->type & SENSOR_TYPE_FAN)
+                               psensor_set_current_value(s, get_fanspeed(s));
+               }
+                       
+                       ss++;
        }
 }
 
@@ -282,8 +277,8 @@ void amd_cleanup()
                dlclose(hdll);
        }
 
-       if (active_amd_adapters) {
-               free(active_amd_adapters);
-               active_amd_adapters = NULL;
+       if (active_adapters) {
+               free(active_adapters);
+               active_adapters = NULL;
        }
 }