-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsmbus.cpp
178 lines (139 loc) · 3.98 KB
/
smbus.cpp
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
#include "smbus.hpp"
#include <errno.h>
#include <fcntl.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include <iostream>
#include <mutex>
#include "i2c.h"
#define MAX_I2C_BUS 30
static constexpr bool DEBUG = false;
static int fd[MAX_I2C_BUS] = {0};
namespace phosphor
{
namespace smbus
{
std::mutex gMutex;
int phosphor::smbus::Smbus::openI2cDev(int i2cbus, char* filename, size_t size,
int quiet)
{
int file;
snprintf(filename, size, "/dev/i2c/%d", i2cbus);
filename[size - 1] = '\0';
file = open(filename, O_RDWR);
if (file < 0 && (errno == ENOENT || errno == ENOTDIR))
{
sprintf(filename, "/dev/i2c-%d", i2cbus);
file = open(filename, O_RDWR);
}
if (DEBUG)
{
if (file < 0 && !quiet)
{
if (errno == ENOENT)
{
fprintf(stderr,
"Error: Could not open file "
"`/dev/i2c-%d' or `/dev/i2c/%d': %s\n",
i2cbus, i2cbus, strerror(ENOENT));
}
else
{
fprintf(stderr,
"Error: Could not open file "
"`%s': %s\n",
filename, strerror(errno));
if (errno == EACCES)
fprintf(stderr, "Run as root?\n");
}
}
}
return file;
}
int phosphor::smbus::Smbus::smbusInit(int smbus_num)
{
int res = 0;
char filename[20];
gMutex.lock();
fd[smbus_num] = openI2cDev(smbus_num, filename, sizeof(filename), 0);
if (fd[smbus_num] < 0)
{
gMutex.unlock();
return -1;
}
res = fd[smbus_num];
gMutex.unlock();
return res;
}
void phosphor::smbus::Smbus::smbusClose(int smbus_num)
{
close(fd[smbus_num]);
}
int phosphor::smbus::Smbus::SendSmbusRWBlockCmdRAW(int smbus_num,
int8_t device_addr,
uint8_t* tx_data,
uint8_t tx_len,
uint8_t* rsp_data)
{
int res, res_len;
unsigned char Rx_buf[I2C_DATA_MAX] = {0};
Rx_buf[0] = 1;
gMutex.lock();
res = i2c_read_after_write(fd[smbus_num], device_addr, tx_len,
(unsigned char*)tx_data, I2C_DATA_MAX,
(unsigned char*)Rx_buf);
if (res < 0)
{
fprintf(stderr, "Error: SendSmbusRWBlockCmdRAW failed\n");
}
res_len = Rx_buf[0] + 1;
memcpy(rsp_data, Rx_buf, res_len);
gMutex.unlock();
return res;
}
int phosphor::smbus::Smbus::set_slave_addr(int file, int address, int force)
{
/* With force, let the user read from/write to the registers
even when a driver is also running */
if (ioctl(file, force ? I2C_SLAVE_FORCE : I2C_SLAVE, address) < 0)
{
fprintf(stderr, "Error: Could not set address to 0x%02x: %s\n", address,
strerror(errno));
return -errno;
}
return 0;
}
int phosphor::smbus::Smbus::GetSmbusCmdByte(int smbus_num, int8_t device_addr,
int8_t smbuscmd)
{
int res;
gMutex.lock();
if (fd[smbus_num] > 0)
{
res = set_slave_addr(fd[smbus_num], device_addr, I2C_SLAVE_FORCE);
if (res < 0)
{
fprintf(stderr,
"set PMBUS BUS%d to slave address 0x%02X failed (%s)\n",
smbus_num, device_addr, strerror(errno));
close(fd[smbus_num]);
gMutex.unlock();
return -1;
}
}
res = i2c_smbus_read_byte_data(fd[smbus_num], smbuscmd);
if (res < 0)
{
fprintf(stderr, "Error: Read failed\n");
gMutex.unlock();
return -1;
}
gMutex.unlock();
return res;
}
} // namespace smbus
} // namespace phosphor