-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathlinux_api.c
More file actions
349 lines (282 loc) · 8.07 KB
/
Copy pathlinux_api.c
File metadata and controls
349 lines (282 loc) · 8.07 KB
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
/*
* Copyright (C) 2009-2017 CompuLab, Ltd.
* Authors: Nikita Kiryanov <nikita@compulab.co.il>
* Igor Grinberg <grinberg@compulab.co.il>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <linux/i2c.h>
#include <linux/i2c-dev.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <time.h>
#include <fcntl.h>
#include <string.h>
#include <errno.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <stdbool.h>
#include "api.h"
#include "common.h"
extern int errno;
/*
* Prepares a device file fd for read or write.
* flags are the standard file open flags.
* On success: returns the fd.
* On failure: -1
*/
static int open_device_file(char *dev_file, int i2c_addr)
{
ASSERT(dev_file);
int fd = open(dev_file, O_RDWR);
if (fd < 0)
return -1;
if (i2c_addr >= 0) {
if (ioctl(fd, I2C_SLAVE_FORCE, i2c_addr) < 0) {
close(fd);
return -1;
}
}
return fd;
}
static inline __s32 i2c_smbus_access(int file, char read_write, __u8 command,
int size, union i2c_smbus_data *data)
{
struct i2c_smbus_ioctl_data args;
args.read_write = read_write;
args.command = command;
args.size = size;
args.data = data;
return ioctl(file, I2C_SMBUS, &args);
}
static int i2c_read(struct api *api, unsigned char *buf, int offset, int size)
{
ASSERT(api && buf);
int bytes_transferred = 0;
union i2c_smbus_data data;
for (int i = offset; i < size; i++) {
if (i2c_smbus_access(api->fd, I2C_SMBUS_READ, i,
I2C_SMBUS_BYTE_DATA, &data) < 0)
return -1;
buf[i] = (unsigned char)(data.byte & 0xFF);
bytes_transferred++;
}
return bytes_transferred;
}
/*
* This function supplies the appropriate delay needed for consecutive writes
* via i2c to succeed
*/
static void msleep(unsigned int msecs)
{
struct timespec time = {0, 1000000 * msecs};
nanosleep(&time, NULL);
}
static int i2c_write(struct api *api, unsigned char *buf, int offset, int size)
{
ASSERT(api && buf);
int bytes_transferred = 0;
union i2c_smbus_data data;
for (int i = offset; i < size; i++) {
data.byte = buf[i];
if (i2c_smbus_access(api->fd, I2C_SMBUS_WRITE, i,
I2C_SMBUS_BYTE_DATA, &data) < 0)
return -1;
msleep(5);
bytes_transferred++;
}
return bytes_transferred;
}
static int driver_read(struct api *api, unsigned char *buf, int offset,
int size)
{
ASSERT(api && buf);
lseek(api->fd, offset, SEEK_SET);
return read(api->fd, buf + offset, size);
}
static int driver_write(struct api *api, unsigned char *buf, int offset,
int size)
{
ASSERT(api && buf);
lseek(api->fd, offset, SEEK_SET);
return write(api->fd, buf + offset, size);
}
static bool i2c_probe(int fd, int addr)
{
union i2c_smbus_data data;
if (ioctl(fd, I2C_SLAVE_FORCE, addr) < 0)
return false;
if (i2c_smbus_access(fd, I2C_SMBUS_READ, 0, I2C_SMBUS_BYTE, &data) < 0)
return false;
return true;
}
#define PRINT_NOT_FOUND(x) eprintf("No "x" was found")
#define PRINT_BUS_NUM(x) (x >= 0) ? eprintf(" on bus %d\n", x) : eprintf("\n")
#define PRINT_DRIVER_HINT(x) eprintf("Is "x" driver loaded?\n")
static int list_i2c_accessible(int bus)
{
ASSERT(bus <= MAX_I2C_BUS);
int fd, ret = -1;
char dev_file_name[13];
bool i2c_bus_found = false;
int i = (bus < 0) ? MIN_I2C_BUS : bus;
int end = (bus < 0) ? MAX_I2C_BUS : bus;
for (; i <= end; i++) {
sprintf(dev_file_name, "/dev/i2c-%d", i);
fd = open(dev_file_name, O_RDWR);
if (fd < 0 && (errno == ENOENT || errno == ENOTDIR))
continue;
i2c_bus_found = true;
if (fd < 0) {
eprintf("Failed accessing I2C bus %d: %s (%d)\n",
i, strerror(errno), -errno);
continue;
}
// return success only if i2c bus exists and can be open
ret = 0;
printf("On i2c-%d:\n\t", i);
for (int j = MIN_I2C_ADDR; j <= MAX_I2C_ADDR; j++)
if (i2c_probe(fd, j))
printf("0x%x ", j);
printf("\n");
close(fd);
}
if (!i2c_bus_found) {
PRINT_NOT_FOUND("I2C device");
PRINT_BUS_NUM(bus);
PRINT_DRIVER_HINT("i2c-dev");
}
return ret;
}
#define DRIVER_DEV_PATH "/sys/bus/i2c/devices"
static int list_driver_accessible(int bus)
{
ASSERT(bus <= MAX_I2C_BUS);
int ret = -1;
char *dev_file_format = DRIVER_DEV_PATH"/%d-00%02x/eeprom";
char dev_file_name[40];
bool driver_found = false;
if (access(DRIVER_DEV_PATH, F_OK) < 0) {
eprintf("Failed accessing path %s: %s (%d)\n",
DRIVER_DEV_PATH, strerror(errno), -errno);
return ret;
}
int i = (bus < 0) ? MIN_I2C_BUS : bus;
int end = (bus < 0) ? MAX_I2C_BUS : bus;
for (; i <= end; i++) {
for (int j = MIN_I2C_ADDR; j <= MAX_I2C_ADDR; j++) {
sprintf(dev_file_name, dev_file_format, i, j);
int res = access(dev_file_name, F_OK);
if (res < 0 && (errno == ENOENT || errno == ENOTDIR))
continue;
driver_found = true;
if (res < 0) {
eprintf("Failed accessing device %s: %s (%d)\n",
dev_file_name, strerror(errno), -errno);
continue;
}
// return success only if device exists and accessible
ret = 0;
printf("EEPROM device file found at: %s\n",
dev_file_name);
}
}
if (!driver_found) {
PRINT_NOT_FOUND("EEPROM device");
PRINT_BUS_NUM(bus);
PRINT_DRIVER_HINT("EEPROM");
}
return ret;
}
static int list_accessible(struct api *api)
{
ASSERT(api);
int ret1, ret2;
printf(COLOR_GREEN "I2C buses:\n" COLOR_RESET);
ret1 = list_i2c_accessible(api->i2c_bus);
printf(COLOR_GREEN "\nEEPROM device files:\n" COLOR_RESET);
ret2 = list_driver_accessible(api->i2c_bus);
return -(ret1 && ret2); /* return -1 only if both fail */
}
static void system_error(const char *message)
{
perror(message);
}
static int setup_interface(struct api *api)
{
ASSERT(api);
ASSERT(api->i2c_bus >= MIN_I2C_BUS && api->i2c_bus <= MAX_I2C_BUS);
ASSERT(api->i2c_addr >= MIN_I2C_ADDR && api->i2c_addr <= MAX_I2C_ADDR);
char i2cdev_fname[13];
char eeprom_dev_fname[40];
int saved_errno;
sprintf(i2cdev_fname, "/dev/i2c-%d", api->i2c_bus);
api->fd = open_device_file(i2cdev_fname, api->i2c_addr);
if (api->fd >= 0) {
api->read = i2c_read;
api->write = i2c_write;
return 0;
}
saved_errno = errno;
sprintf(eeprom_dev_fname, DRIVER_DEV_PATH"/%d-00%02x/eeprom",
api->i2c_bus, api->i2c_addr);
api->fd = open_device_file(eeprom_dev_fname, -1);
if (api->fd < 0) {
/* print error which occurred when opening i2c-dev file */
eprintf("Error, %s access failed: %s (%d)\n",
i2cdev_fname, strerror(saved_errno), -saved_errno);
if (saved_errno == ENOENT)
PRINT_DRIVER_HINT("i2c-dev");
/* print error which occurred when opening eeprom-dev file */
eprintf("Error, %s access failed: %s (%d)\n",
eeprom_dev_fname, strerror(errno), -errno);
if (errno == ENOENT)
PRINT_DRIVER_HINT("EEPROM");
} else {
api->read = driver_read;
api->write = driver_write;
return 0;
}
eprintf("Neither EEPROM driver nor i2c device interface is available");
eprintf("\n");
return -1;
}
static int api_read_before_setup(struct api *api, unsigned char *buf,
int offset, int size)
{
ASSERT(api);
// Setup sets the api->read and api->write function pointers on success
if (setup_interface(api) == 0)
return api->read(api, buf, offset, size);
return -1;
}
static int api_write_before_setup(struct api *api, unsigned char *buf,
int offset, int size)
{
ASSERT(api);
// Setup sets the api->read and api->write function pointers on success
if (setup_interface(api) == 0)
return api->write(api, buf, offset, size);
return -1;
}
void api_init(struct api *api, int i2c_bus, int i2c_addr)
{
api->i2c_bus = i2c_bus;
api->i2c_addr = i2c_addr;
api->read = api_read_before_setup;
api->write = api_write_before_setup;
api->probe = list_accessible;
api->system_error = system_error;
}