File: getMBinfo.c

package info (click to toggle)
xmbmon 2.05-9
  • links: PTS
  • area: main
  • in suites: experimental
  • size: 920 kB
  • sloc: ansic: 6,089; sh: 2,156; perl: 87; makefile: 80
file content (496 lines) | stat: -rw-r--r-- 10,849 bytes parent folder | download | duplicates (5)
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
/************************************************
	Subroutines to get Mother Board Information

	Information related to Winbond W83781D Chip
		and National Semiconductor LM78/LM79 Chips
	by Alex van Kaam

	Information for VIA VT82C686A/B
	by ":p araffin.(Yoneya)", MANY THANKS!!

	Information for SMBus access
	by Linux lm_sensor homepage, MANY THANKS!!
		http://www.netroedge.com/~lm78/

 ************************************************/

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <string.h>
#include <stdio.h>
#include "pci_pm.h"
#include "smbuses.h"
#include "methods.h"
#include "sensors.h"
#include "smb_extemp.h"

#ifdef TEMP_LIMIT
#define TEMP_HIGH 100.0
#define TEMP_LOW  -10.0
#endif

/* external (global) data */
extern int debug_flag, fahrn_flag;
extern int TyanTigerMP_flag;
extern int isa_port_base;
extern int viahwm_base, smb_base;
extern int pm_smb_detected;
extern int smb_slave;
extern char *probe_request;
extern SENSOR *HWM_module[];
extern char *HWM_name[];
extern int HWM_VIA, HWM_SMB, HWM_ISA;
extern int HWM_SMBchip[];
extern int HWM_smbslave[];
extern int HWM_ISAchip[];
extern int numSMBSlave;
extern int canSMBSlave[];
extern int num_extemp_chip;
extern int smb_extemp_chip[];
extern int smb_extemp_slave[];
extern int extra_tempNO;

/* Access method functions, global */
extern LM_METHODS method_isa, method_smb, method_via;


static LM_METHODS *this_method = NULL;
static SENSOR *this_sensor = NULL;
static int probe_flag = SEARCH;
static int HWM_firstSMB_flag = 0;
static char method;

/* function declarations */
extern void TyanTigerMPinit(void);
int Probe_method(void);
int HWM_detection(int);
void HWM_set_firstSMB(SENSOR *, int);
int probe_HWMChip(LM_METHODS *, int);
int via_set(void);
int smb_set(int, int);
int isa_set(int);
int InitMBInfo(char);
int getTemp(float *, float *, float *);
int getVolt(float *, float *, float *, float *, float *, float *, float *);
int getFanSp(int *, int *, int *);


/*----------------------
	Detecting HWM Chip
  ----------------------*/

int Probe_method(void)
{
	int n;

	if (method != 'I') {
		pm_smb_detected = set_smbus_io(&viahwm_base, &smb_base);
	}
	if (method == 'V') {
  		if ((n = pm_smb_detected) == VIA686HWM) {
		/* VIA VT82C686 HWM is available */
			return via_set();
		} else if (n != -1) {
			fprintf(stderr, "No VIA686 HWM available!!\n");
			return 1;
		}
	} else if (method == 'S') {
  		if ((n = pm_smb_detected) > 0) {
		/* SMBus PowerManagement, hardware monitor exist ? */
			return smb_set(probe_flag, n);
		} else if (n != -1) {
			fprintf(stderr, "No SMBus HWM available!!\n");
			return 1;
		}
	} else if (method == 'I') {
		/* Just try ISA-IO method */
		if ((n = isa_set(probe_flag)) == 0) {
			return 0;
		} else if (n != -1) {
			fprintf(stderr, "No ISA-IO HWM available!!\n");
			return 1;
		}
	} else {
	/* No input method option: Try probing each HWM type */
	  	if ((probe_flag == SEARCH || probe_flag == c_via686)
					&& (n = pm_smb_detected) == VIA686HWM) {
			if (via_set() == 0 && method != 'A')
				return 0;
			else if (n > 0)
				goto smb_chk;
		} else if ((n = pm_smb_detected) > 0) {
smb_chk:	if (smb_set(probe_flag, n) == 0 && method != 'A')
				return 0;
			goto isa_chk;
		} else {
isa_chk:	if ((n = isa_set(probe_flag)) == 0 && method != 'A')
				return 0;
			else if (n != -1 && method != 'A') {
				fprintf(stderr, "No Hardware Monitor found!!\n");
				return 1;
			}
		}
	}

	if (method == 'A') {
		return HWM_detection(pm_smb_detected);
	}

	return -1;
}

int HWM_detection(int chip)
{
	int n, j, k, ext;

	if (debug_flag)
		fprintf(stderr, "Summary of Detection:\n");

	if (HWM_VIA + HWM_SMB + HWM_ISA <= 0) {
		if (debug_flag)
			fprintf(stderr, " * No monitors found.\n");
		return -1;
	}

	if (HWM_VIA > 0) {
		this_method = &method_via;
		if (debug_flag)
			fprintf(stderr, " * VIA686A/B monitor found.\n");
	}
	for (n = 0, k = 0; HWM_module[n] != NULL; n++) {
		if (HWM_SMBchip[n] != 0) {
			if (debug_flag) {
				if (!k)
#if !defined(LINUX) && defined(HAVE_SMBUS) && defined(SMBUS_IOCTL)
					fprintf(stderr, " * SMB monitor(s)[ioctl:%s]:\n",
#else		/* SMBus direct access routines */
					fprintf(stderr, " * SMB monitor(s)[%s]:\n",
#endif
						chk_smb_chip(chip));
				fprintf(stderr, "  ** %s found at slave address: 0x%02X.\n",
					HWM_module[n]->Name, HWM_smbslave[n]);
			}
			k++;
			ext = 0;
			j = num_extemp_chip;
			if (!strcmp(HWM_name[n], "lm75")) {
				smb_extemp_chip[j] = ex_lm75;
				ext = 1;
			} else if (!strcmp(HWM_name[n], "lm90")) {
				smb_extemp_chip[j] = ex_lm90;
				ext = 1;
			} else if (!strcmp(HWM_name[n], "wl784")
						&& HWM_SMBchip[n] == W83L785TS) {
				smb_extemp_chip[j] = ex_wl785ts;
				ext = 1;
			}
			if (ext) {
				smb_extemp_slave[j] = HWM_smbslave[n];
				num_extemp_chip++;
			}
			/* set the first SMB monitor found */
			if (!ext && !HWM_firstSMB_flag && HWM_SMB > 1)
				HWM_set_firstSMB(HWM_module[n], HWM_smbslave[n]);
		}
	}
	for (n = 0, k = 0; HWM_module[n] != NULL; n++) {
		if (HWM_ISAchip[n] != 0) {
			if (debug_flag) {
				if (!k)
					fprintf(stderr, " * ISA monitor(s):\n");
				fprintf(stderr, "  ** %s found.\n", HWM_module[n]->Name);
			}
			k++;
		}
	}
#ifdef DEBUG
printf("HWM_VIA=%d, HWM_SMB=%d, HWM_ISA=%d\n", HWM_VIA, HWM_SMB, HWM_ISA);
#endif
	if (HWM_VIA + HWM_SMB + HWM_ISA == num_extemp_chip)
		num_extemp_chip = 0;

	return 0;
}

void HWM_set_firstSMB(SENSOR *module, int slave)
{
	int n;

	for (n = 0; n < numSMBSlave; n++) {
		if (canSMBSlave[n] == 0xFF) {
			canSMBSlave[n] = slave;
			break;
		}
	}

	this_method = &method_smb;
	this_sensor = module;

	n = debug_flag;
	debug_flag = 0;
	{
		this_method->Open();
		this_sensor->Probe(this_method);
		this_method->Close();
	}
	debug_flag = n;
	HWM_firstSMB_flag = 1;
}

int probe_HWMChip(LM_METHODS *methodp, int probe)
{
	int i, n, n0 = 0, num = 0;

	if (methodp->Open() != 0)
		return -1;

	/* module order is important !! */
	for (n = 0; HWM_module[n] != NULL; n++) {
		if ((probe == SEARCH || probe == n)
				&& (i = HWM_module[n]->Probe(methodp)) != 0) {
			if (methodp == &method_smb) {
				HWM_SMBchip[n] = i;
				HWM_smbslave[n] = smb_slave;
				HWM_SMB++;
			} else if (methodp == &method_isa) {
				HWM_ISAchip[n] = i;
				HWM_ISA++;
			} else {
				HWM_VIA++;
			}
			num++;
			if (n0 == 0)
				n0 = n;
			if (method != 'A')
				break;
		}
	}
	if (num)
		this_sensor = HWM_module[n0];	/* set HWM found first */

	methodp->Close();
	return num;
}

int via_set(void)
{
	if (debug_flag > 1)
		fprintf(stderr, ">>> Testing Reg's at VIA686 HWM <<<\n");

	if (probe_HWMChip(&method_via, c_via686) > 0) {
		this_method = &method_via;
		if (debug_flag && method != 'A')
			fprintf(stderr, "Using VIA686 HWM directly!!\n");
		return 0;
	} else {
		fprintf(stderr, "Something Wrong in detected VIA686 HWM!!\n");
		return 1;
	}
}

int smb_set(int probe, int chip)
{
	if (debug_flag > 1) {
#if !defined(LINUX) && defined(HAVE_SMBUS) && defined(SMBUS_IOCTL)
		fprintf(stderr, ">>> Testing Reg's at SMBus <<<\n");
#else		/* SMBus direct access routines */
		fprintf(stderr, ">>> Testing Reg's at SMBus <<<\n"\
			"[%s, IO-Base:0x%0X]\n", chk_smb_chip(chip), smb_base);
#endif
	}
	if (find_smb_dev() <= 0)
		goto ret1;

	if (probe_HWMChip(&method_smb, probe) > 0) {
		this_method = &method_smb;
		if (debug_flag && method != 'A') {
#if !defined(LINUX) && defined(HAVE_SMBUS) && defined(SMBUS_IOCTL)
			fprintf(stderr, "Using SMBus-ioctl access method[%s]!!\n",
#else		/* SMBus direct access routines */
			fprintf(stderr, "Using SMBus access method[%s]!!\n",
#endif
				chk_smb_chip(chip));
		}
		return 0;
	} else {
ret1:	if (debug_flag) {
			fprintf(stderr, "SMBus[%s] found, but No HWM available on it!!\n",
				chk_smb_chip(chip));
		}
		return 1;
	}
}

int isa_set(int probe)
{
	int n;

	if (debug_flag > 1)
		fprintf(stderr, ">>> Testing Reg's at ISA-IO <<<\n"\
			"[ISA Port IO-Base:0x%0X]\n", isa_port_base);

	if ((n = probe_HWMChip(&method_isa, probe)) > 0) {
		this_method = &method_isa;
		if (debug_flag && method != 'A')
			fprintf(stderr, "Using ISA-IO access method!!\n");
		return 0;
	} else if (n == 0)
		n = 1;

	return n;
}

int InitMBInfo(char method_inp)
{
	int n;

 	/* this is TyanTigerMP specific treatment */
	if (TyanTigerMP_flag)
	 	TyanTigerMPinit();

	if (debug_flag > 1)
		fprintf(stderr, "Probe Request: %s\n", probe_request);

	for (n = 0; HWM_name[n] != NULL; n++) {
		if (strcmp(probe_request, HWM_name[n]) == 0) {
			probe_flag = n;
			break;
		}
	}

	method = method_inp;
	if ((n = Probe_method()) != 0) {
		return n;
	}

	if (this_method && this_sensor) {
		if (debug_flag && method != 'A')
			fprintf(stderr, "* %s found.\n", this_sensor->Name);
		return 0;
	} else {
		return -1;
	}
}


/*-------------------------
	Getting Temperatures
  -------------------------*/

#define traFahrn(x)	((x) * 1.8 + 32.0)

int getTemp(float *t1, float *t2, float *t3)
{
	int n;
	float f, t[3] = {0.0, 0.0, 0.0};

	if (this_method->Open() != 0)
		return -1;

	if (this_sensor) {
		for (n = 0; n < 3; n++) {
			if ((f = this_sensor->Temp(this_method, n)) != 0xFFFF) {
#ifdef TEMP_LIMIT
			if (f <= TEMP_HIGH || f >= TEMP_LOW)
#endif
				t[n] = f;
			}
		}
  	}
	if (this_sensor == &it87 || this_sensor == &via686) {
	/* special treatment of IT86 and VIA868 */
		f = t[0];
		t[0] = t[1];
		t[1] = t[2];
		t[2] = f;
	} else if (this_sensor == &lm85) {
	/* special treatment of LM85 */
		f = t[0];
		t[0] = t[1];
		t[1] = f;
	}

	if (num_extemp_chip > 0) {
		if ((f = smb_ExtraTemp()) != 0xFFFF)
			t[extra_tempNO] = f;
	}

	if (fahrn_flag) {
		for (n = 0; n <= 2; n++) {
			if (t[n] != 0.0)
				t[n] = traFahrn(t[n]);
		}
	}

	*t1 = t[0]; *t2 = t[1]; *t3 = t[2];

	this_method->Close();
	return 0;
}

/*--------------------
	Getting Voltages
  --------------------*/

int getVolt(float *vc0, float *vc1,\
			float *v33, float *v50p, float *v50n,\
			float *v12p, float *v12n)
{
	int n;
	float f, v[7] = {0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0};

	if (this_method->Open() != 0)
		return -1;

	if (this_sensor) {
		for (n = 0; n < 7; n++) {
			if ((f = this_sensor->Volt(this_method, n)) != 0xFFFF)
				v[n] = f;
		}
	}
	if (this_sensor == &lm85) {
	/* special treatment of LM85 */
		f = v[0];
		v[0] = v[1];
		v[1] = f;
	}

	*vc0 = v[0], *vc1 = v[1];
	*v33 = v[2], *v50p = v[3], *v12p = v[4];
	*v12n = v[5], *v50n = v[6];

	this_method->Close();
	return 0;
}

/*----------------------
	Getting Fan Speed
  ----------------------*/

int getFanSp(int *r1, int *r2, int *r3)
{
	int n;
	int i, r[3] = {0,0,0};

	if (this_method->Open() != 0)
		return -1;

	if (this_sensor) {
		for (n = 0; n < 3; n++) {
			if ((i = this_sensor->FanRPM(this_method, n)) != 0xFFFF)
				r[n] = i;
		}
	}
	if (this_sensor == &lm85) {
	/* special treatment of LM85 */
		i = r[0];
		r[0] = r[1];
		r[1] = i;
	}
	*r1 = r[0]; *r2 = r[1]; *r3 = r[2];

	this_method->Close();
	return 0;
}