File: getMB-smb_ioctl.c

package info (click to toggle)
xmbmon 2.05-6
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd, lenny, squeeze, wheezy
  • size: 752 kB
  • ctags: 907
  • sloc: ansic: 6,089; sh: 2,923; makefile: 141; perl: 87
file content (189 lines) | stat: -rw-r--r-- 3,467 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
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#ifdef HAVE_SMBUS
/* assume SMBus ioctl support, only for FreeBSD */

#include <stdlib.h>
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/ioctl.h>

#include <machine/smb.h>

#include "methods.h"

/* SMBus Base Address, global */
int smb_base	= 2002;		/* arbitrary positive number */
int smb_slave	= LM_ADDR;
int smb_wbtemp1_flag = 1;	/* = 0 if enable */
int smb_wbtemp2_flag = 1;	/* = 0 if eaable */
int smb_wbtemp1	= WBtemp1_ADDR;
int smb_wbtemp2	= WBtemp2_ADDR;
int smb_wbt1reg	= 0x00;
int smb_wbt2reg	= 0x00;

/* SMBus device file name, global */
extern char *smb_devfile;

int smbioctl_readB(int, int);
void smbioctl_writeB(int, int, int);
int smbioctl_readW(int, int);
void smbioctl_writeW(int, int, int);

static int iosmb;
static char buf[128];

#ifndef NO_INCLUDE_SMBIO

static int OpenIO()
{
	char byte;
	struct smbcmd cmd;
	cmd.slave = smb_slave;
	cmd.data.byte_ptr = &byte;

	if ((iosmb = open(smb_devfile, 000)) < 0) {
		strcpy(buf, "ioctl(");
		strcat(buf, smb_devfile + 5);
		strcat(buf, ":open)");
		perror(buf);
		return -1;
	}
/* command Reg:0x47 is not universal for all hardware monitor chips */
#if 0
	cmd.cmd = 0x47;
	if (ioctl(iosmb, SMB_READB, &cmd) == -1) {
		strcpy(buf, "ioctl(");
		strcat(buf, smb_devfile + 5);
		strcat(buf, ":read Reg.0x47)");
		perror(buf);
		return -1;
	}
#endif
	return 0;
}

static void CloseIO()
{
	close(iosmb);
}

static int ReadByte(int addr)
{
	return smbioctl_readB(smb_slave, addr);
}

static void WriteByte(int addr, int value)
{
	smbioctl_writeB(smb_slave, addr, value);
}

static int ReadWord(int addr)
{
	return smbioctl_readW(smb_slave, addr);
}

static void WriteWord(int addr, int value)
{
	smbioctl_writeW(smb_slave, addr, value);
}

static int ReadTemp1()
{
	return smbioctl_readW(smb_wbtemp1, smb_wbt1reg);
}

static int ReadTemp2()
{
	return smbioctl_readW(smb_wbtemp2, smb_wbt2reg);
}

struct lm_methods method_smb = {
  OpenIO,
  CloseIO,
  ReadByte,
  WriteByte,
  ReadWord,
  WriteWord,
  ReadTemp1,
  ReadTemp2
};

#endif


/* From here global routines using smb_ioctl */

int smbioctl_readB(int slave, int addr)
{
	struct smbcmd cmd;
	char ret;
	cmd.slave = slave;
	cmd.cmd = addr;
	cmd.data.byte_ptr = &ret;
	if (ioctl(iosmb, SMB_READB, &cmd) == -1) {
/*		strcpy(buf, "ioctl(");
		strcat(buf, smb_devfile + 5);
		strcat(buf, ":readbyte)");
		perror(buf);
		exit(-1); */
		ret = 0xFF;
	}
	return (ret & 0xFF);
}

void smbioctl_writeB(int slave, int addr, int value)
{
	struct smbcmd cmd;
	cmd.slave = slave;
	cmd.cmd = addr;
	cmd.data.byte = value;
	if (ioctl(iosmb, SMB_WRITEB, &cmd) == -1) {
		strcpy(buf, "ioctl(");
		strcat(buf, smb_devfile + 5);
		strcat(buf, ":writebyte)");
		perror(buf);
		exit(-1);
	}
}

int smbioctl_readW(int smb_slave, int addr)
{
	struct smbcmd cmd;
	short ret;
	cmd.slave = smb_slave;
	cmd.cmd = addr;
	cmd.data.word_ptr = &ret;
	if (ioctl(iosmb, SMB_READW, &cmd) == -1) {
/*		strcpy(buf, "ioctl(");
		strcat(buf, smb_devfile + 5);
		strcat(buf, ":readword)");
		perror(buf);
		exit(-1); */
		ret = 0xFFFF;
	}
	return (ret & 0xFFFF);
}

void smbioctl_writeW(int slave, int addr, int value)
{
	struct smbcmd cmd;
	cmd.slave = slave;
	cmd.cmd = addr;
	cmd.data.word = value;
	if (ioctl(iosmb, SMB_WRITEW, &cmd) == -1) {
		strcpy(buf, "ioctl(");
		strcat(buf, smb_devfile + 5);
		strcat(buf, ":writeword)");
		perror(buf);
		exit(-1);
	}
}


#endif