File: sensors.c

package info (click to toggle)
xmbmon 2.05-8
  • links: PTS
  • area: main
  • in suites: bullseye, buster, sid, stretch
  • size: 928 kB
  • ctags: 910
  • sloc: ansic: 6,089; sh: 2,151; perl: 87; makefile: 79
file content (203 lines) | stat: -rw-r--r-- 4,341 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
/* functions used commonly in each hardware monitor module */

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

#ifdef DEBUG
#define SMB_DEBUG
#endif

#include <stdio.h>
#define INCLUDE_HWM_MODULE
#include "sensors.h"
#undef INCLUDE_HWM_MODULE

/* external (global) data */
extern int debug_flag;
extern int smb_base;
extern int smb_slave;
extern int smb_wbtemp1, smb_wbtemp2;
extern LM_METHODS method_smb;

/* SMBus Slave Address Candidates, global */
int numSMBSlave	= 0;
int canSMBSlave[128];


int chkReg_Probe(int slave, char *comment, int Reg[], LM_METHODS *method)
{
	int i, n, r, ret = 0;

	if (slave > 0 && debug_flag > 1)
		fprintf(stderr, "Set SMBus slave address: 0x%02X\n", slave);
	if (debug_flag > 1)
		fprintf(stderr, "%s", comment);
	for (i = 0; (r = Reg[i]) != -1; i++) {
		n = method->Read(r);
		if (n != 0xFF)
			++ret;
		if (debug_flag > 1) {
			if ((i + 1) % 4 == 0)
				fprintf(stderr, "  CR%02X:0x%02X\n", r, n);
			else
				fprintf(stderr, "  CR%02X:0x%02X,", r, n);
		}
	}
	if (debug_flag > 1) {
		if(( i & 3 ) != 0 )
			fprintf( stderr, "\n" ) ;
	}
#if 0
	if (debug_flag > 1)
		fprintf(stderr, "\n");
#endif

	return ret;
}

int chkReg_Probe_strict(int Reg[], LM_METHODS *method)
{
	int i, r;

	for (i = 0; (r = Reg[i]) != -1; i++) {
		if (method->Read(r) == 0xFF)
			return 0;
	}
	return 1;
}


/* Register checked for scanning smbus */
static int smb_scanReg[] = {
	0x00, 0x01, 0x20, 0x40, 0x48,
	-1 };

/* Blacklist slave address, do not access */
static int blacklist[] = {
	0xD2,
	-1 };

int scan_smbus(int addr_start, int addr_end, int result[])
{
	int i, j, n, r, save, reg, ret = 0;
	LM_METHODS *method = &method_smb;

#if !defined(LINUX) && defined(HAVE_SMBUS) && defined(SMBUS_IOCTL)
#else
	if (smb_base <= 0)
		return ret;
#endif

	addr_start = 2 * (addr_start/2);
	if (addr_start <= 0)
		addr_start = 2;
	if (addr_end > 0xFE)
		addr_end = 0xFE;

	save = smb_slave;
	method->Open();
	for (j = addr_start, ret = 0; j <= addr_end; j += 2) {
		smb_slave = j;
		for (i = 0; blacklist[i] != -1; i++)
			if (j == blacklist[i])
				goto skip;
#ifdef SMB_DEBUG
if (debug_flag > 1)
fprintf(stderr, "SLAVE::0x%02X\n", j);
#endif
		for (i = 0, n = 0; (r = smb_scanReg[i]) != -1; i++) {
			/* != 0xFF is not enough; need to check more, but ... */
			if (((reg = method->Read(r)) & 0xFF) != 0xFF)
				++n;
#ifdef SMB_DEBUG
if (debug_flag > 1)
fprintf(stderr, "  0x%02X:0x%02X\n", r, reg);
#endif
		}
		if (n) {
			result[ret++] = j;
			if (debug_flag > 1)
				fprintf(stderr,
			" SMBus slave 0x%02X(0x%02X) found...\n", j, j/2);
		}
skip:
		continue;
	}
	method->Close();
	smb_slave = save;
	return ret;
}

int find_smb_dev(void)
{
	return (numSMBSlave = scan_smbus(0x00, 0xFE, canSMBSlave));
}

int get_smb_slave(int start, int end)
{
	int i;
	for (i = 0; i < numSMBSlave; i++) {
		if (start <= canSMBSlave[i] && canSMBSlave[i] <= end)
			break;
	}
	if (i >= numSMBSlave)
		return 0;
	else
		return canSMBSlave[i];
}

void kill_smb_slave(int slave)
{
	int i;
	for (i = 0; i < numSMBSlave; i++) {
		if (slave == canSMBSlave[i]) {
			canSMBSlave[i] = 0xFF;
			break;
		}
	}
}

int set_smb_Extemp(int start, int end, int *temp1, int *temp2)
{
    int i, temp1_flag = 1, temp2_flag = 1;    /* disable! */

    for (i = 0; i < numSMBSlave; i++) {
        if (start <= canSMBSlave[i] && canSMBSlave[i] <= end) {
            if (temp1_flag) {
                temp1_flag = 0; /* enabled! */
                *temp1 = canSMBSlave[i];
            } else if (temp2_flag) {
                temp2_flag = 0; /* enabled! */
                *temp2 = canSMBSlave[i];
            }
        }
    }
	return ((temp1_flag << 1) + temp2_flag);
}

void info_Extemp(LM_METHODS *method, int temp1, int temp2)
{
    if (!temp1) {
        if (debug_flag > 1) {
			if (method == &method_smb)
	            fprintf(stderr,"* Temp1 exists at 0x%02X,", smb_wbtemp1);
			else
	            fprintf(stderr,"* Temp1 exists at Bank 1,");
    	}
    } else {
        if (debug_flag > 1)
            fprintf(stderr,"* NO Temp1,");
    }
    if (!temp2) {
        if (debug_flag > 1) {
			if (method == &method_smb)
        	    fprintf(stderr," Temp2 exists at 0x%02X.\n", smb_wbtemp2);
			else
	            fprintf(stderr," Temp2 exists at Bank 2.\n");
    	}
    } else {
        if (debug_flag > 1)
            fprintf(stderr," NO Temp2.\n");
    }
}