fixed realloc cppcheck warning
[psensor.git] / src / lib / amd.c
1 /*
2  * Copyright (C) 2010-2011 thgreasi@gmail.com, jeanfi@gmail.com
3  * Copyright (C) 2010-2012 jeanfi@gmail.com
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License as
7  * published by the Free Software Foundation; either version 2 of the
8  * License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18  * 02110-1301 USA
19  */
20 #ifndef LINUX
21 #define LINUX 1
22 #endif
23 #ifdef HAVE_LIBATIADL
24         /* AMD id for the aticonfig */
25         int amd_id;
26 #endif
27
28 #include <locale.h>
29 #include <libintl.h>
30 #define _(str) gettext(str)
31
32 #include <dlfcn.h>
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <string.h>
36
37 #include <adl_sdk.h>
38
39 #include "psensor.h"
40
41 typedef int (*ADL_MAIN_CONTROL_CREATE)(ADL_MAIN_MALLOC_CALLBACK, int);
42 typedef int (*ADL_MAIN_CONTROL_DESTROY)();
43 typedef int (*ADL_ADAPTER_NUMBEROFADAPTERS_GET) (int *);
44 typedef int (*ADL_ADAPTER_ADAPTERINFO_GET) (LPAdapterInfo, int);
45 typedef int (*ADL_ADAPTER_ACTIVE_GET) (int, int*);
46 typedef int (*ADL_OVERDRIVE5_TEMPERATURE_GET) (int, int, ADLTemperature*);
47 typedef int (*ADL_OVERDRIVE5_FANSPEED_GET) (int, int, ADLFanSpeedValue*);
48
49 static ADL_MAIN_CONTROL_CREATE            adl_main_control_create;
50 static ADL_MAIN_CONTROL_DESTROY           adl_main_control_destroy;
51 static ADL_ADAPTER_NUMBEROFADAPTERS_GET   adl_adapter_numberofadapters_get;
52 static ADL_ADAPTER_ADAPTERINFO_GET        adl_adapter_adapterinfo_get;
53 static ADL_ADAPTER_ACTIVE_GET             adl_adapter_active_get;
54 static ADL_OVERDRIVE5_TEMPERATURE_GET     adl_overdrive5_temperature_get;
55 static ADL_OVERDRIVE5_FANSPEED_GET        adl_overdrive5_fanspeed_get;
56
57 static void *hdll;
58 static int adl_main_control_done;
59 static int *active_adapters;
60
61 static void __stdcall *adl_main_memory_alloc(int isize)
62 {
63         void *lpbuffer = malloc(isize);
64         return lpbuffer;
65 }
66
67 static void *getprocaddress(void *plibrary, const char *name)
68 {
69         return dlsym(plibrary, name);
70 }
71
72 /*
73   Returns the temperature (Celcius) of an AMD/Ati GPU.
74 */
75 static double get_temp(struct psensor *sensor)
76 {
77         ADLTemperature temperature;
78
79         temperature.iSize = sizeof(ADLTemperature);
80         temperature.iTemperature = -273;
81         if (ADL_OK != adl_overdrive5_temperature_get(sensor->amd_id,
82                  0, &temperature))
83                 return UNKNOWN_DBL_VALUE;
84
85         return temperature.iTemperature/1000;
86 }
87
88 static double get_fanspeed(struct psensor *sensor)
89 {
90         ADLFanSpeedValue fanspeedvalue;
91
92         fanspeedvalue.iSize = sizeof(ADLFanSpeedValue);
93         fanspeedvalue.iSpeedType = ADL_DL_FANCTRL_SPEED_TYPE_RPM;
94         fanspeedvalue.iFanSpeed = -1;
95         if (ADL_OK != adl_overdrive5_fanspeed_get(sensor->amd_id,
96                  0, &fanspeedvalue))
97                 return UNKNOWN_DBL_VALUE;
98
99         return fanspeedvalue.iFanSpeed;
100 }
101
102 static struct psensor *create_sensor(int id, int values_len)
103 {
104         char name[200];
105         char *sid;
106         int sensor_type;
107
108         struct psensor *s;
109
110         if (id & 1) {/* odd number ids represent fan sensors */
111                 id = id >> 1;
112                 sprintf(name, "GPU%dfan", id);
113                 sensor_type = SENSOR_TYPE_AMD_FAN;
114         } else {/* even number ids represent temperature sensors */
115                 id = id >> 1;
116                 sprintf(name, "GPU%dtemp", id);
117                 sensor_type = SENSOR_TYPE_AMD_TEMP;
118         }
119
120         sid = malloc(strlen("amd") + 1 + strlen(name) + 1);
121         sprintf(sid, "amd %s", name);
122
123         s = psensor_create(sid, strdup(name), strdup("ATI GPU"),
124                            sensor_type, values_len);
125
126         s->amd_id = active_adapters[id];
127
128         return s;
129 }
130
131 /*
132   Returns the number of AMD/Ati GPU sensors (temperature and fan
133   speed).
134
135   Return 0 if no AMD/Ati gpus or cannot get information.
136 */
137 static int init()
138 {
139         LPAdapterInfo lpadapterinfo = NULL;
140         int i, inumberadapters, inumberadaptersactive = 0;
141         int lpstatus, iadapterindex;
142
143         hdll = NULL;
144         adl_main_control_done = 0;
145         active_adapters = NULL;
146         hdll = dlopen("libatiadlxx.so", RTLD_LAZY|RTLD_GLOBAL);
147
148         if (!hdll) {
149                 log_err(_("AMD: cannot found ADL library."));
150                 return 0;
151         }
152
153         adl_main_control_create = (ADL_MAIN_CONTROL_CREATE)
154                  getprocaddress(hdll, "ADL_Main_Control_Create");
155         adl_main_control_destroy = (ADL_MAIN_CONTROL_DESTROY)
156                  getprocaddress(hdll, "ADL_Main_Control_Destroy");
157         adl_adapter_numberofadapters_get = (ADL_ADAPTER_NUMBEROFADAPTERS_GET)
158                  getprocaddress(hdll, "ADL_Adapter_NumberOfAdapters_Get");
159         adl_adapter_adapterinfo_get = (ADL_ADAPTER_ADAPTERINFO_GET)
160                  getprocaddress(hdll, "ADL_Adapter_AdapterInfo_Get");
161         adl_adapter_active_get = (ADL_ADAPTER_ACTIVE_GET)
162                  getprocaddress(hdll, "ADL_Adapter_Active_Get");
163         adl_overdrive5_temperature_get = (ADL_OVERDRIVE5_TEMPERATURE_GET)
164                  getprocaddress(hdll, "ADL_Overdrive5_Temperature_Get");
165         adl_overdrive5_fanspeed_get = (ADL_OVERDRIVE5_FANSPEED_GET)
166                  getprocaddress(hdll, "ADL_Overdrive5_FanSpeed_Get");
167         if (!adl_main_control_create ||
168                 !adl_main_control_destroy ||
169                 !adl_adapter_numberofadapters_get ||
170                 !adl_adapter_adapterinfo_get ||
171                 !adl_overdrive5_temperature_get ||
172                 !adl_overdrive5_fanspeed_get) {
173                 log_err(_("AMD: missing ADL's API."));
174                 return 0;
175         }
176
177         if (ADL_OK != adl_main_control_create(adl_main_memory_alloc, 1)) {
178                 log_err(_("AMD: failed to initialize ADL."));
179                 return 0;
180         }
181         adl_main_control_done = 1;
182
183         if (ADL_OK != adl_adapter_numberofadapters_get(&inumberadapters)) {
184                 log_err(_("AMD: cannot get the number of adapters."));
185                 return 0;
186         }
187
188         if (!inumberadapters)
189                 return 0;
190
191         lpadapterinfo = malloc(sizeof(AdapterInfo) * inumberadapters);
192         memset(lpadapterinfo, '\0', sizeof(AdapterInfo) * inumberadapters);
193
194         adl_adapter_adapterinfo_get(lpadapterinfo,
195                                     sizeof(AdapterInfo) * inumberadapters);
196
197         for (i = 0; i < inumberadapters; i++) {
198
199                 iadapterindex = lpadapterinfo[i].iAdapterIndex;
200
201                 if (ADL_OK != adl_adapter_active_get(iadapterindex, &lpstatus))
202                         continue;
203                 if (lpstatus != ADL_TRUE)
204                         continue;
205
206                 if (!active_adapters) {
207                         active_adapters = (int *) malloc(sizeof(int));
208                         inumberadaptersactive = 1;
209                 } else {
210                         ++inumberadaptersactive;
211                         active_adapters = (int *)realloc
212                                 (active_adapters,
213                                  sizeof(int)*inumberadaptersactive);
214
215                         if (!active_adapters)
216                                 exit(EXIT_FAILURE);
217                 }
218                 active_adapters[inumberadaptersactive-1] = iadapterindex;
219         }
220
221         free(lpadapterinfo);
222
223         /* Each Adapter has one temperature sensor and one fan */
224         return 2*inumberadaptersactive;
225 }
226
227 void amd_psensor_list_update(struct psensor **sensors)
228 {
229         struct psensor **ss, *s;
230
231         ss = sensors;
232         while (*ss) {
233                 s = *ss;
234
235                 if (s->type == SENSOR_TYPE_AMD_TEMP)
236                         psensor_set_current_value(s, get_temp(s));
237                 else if (s->type == SENSOR_TYPE_AMD_FAN)
238                         psensor_set_current_value(s, get_fanspeed(s));
239
240                 ss++;
241         }
242 }
243
244 struct psensor * *
245 amd_psensor_list_add(struct psensor **sensors, int values_len)
246 {
247         int i, n;
248         struct psensor **tmp, **ss, *s;
249
250         n = init();
251
252         ss = sensors;
253         for (i = 0; i < n; i++) {
254                 s = create_sensor(i, values_len);
255
256                 tmp = psensor_list_add(ss, s);
257
258                 if (ss != tmp)
259                         free(ss);
260
261                 ss = tmp;
262         }
263
264         return ss;
265 }
266
267 void amd_cleanup()
268 {
269         if (hdll) {
270                 if (adl_main_control_done)
271                         adl_main_control_destroy();
272                 dlclose(hdll);
273         }
274
275         if (active_adapters) {
276                 free(active_adapters);
277                 active_adapters = NULL;
278         }
279 }