IBASE SMARC-EVK1 User Manual

Page 77

Advertising
background image

Copyright © 2013 IBASE Technology Inc. All Rights Reserved.

51

IBASE Technology Inc.

s[i] = '\0';
return i + 2;
}

void free_adapters(struct i2c_adap *adapters)
{
int i;

for (i = 0; adapters[i].name; i++)
free(adapters[i].name);
free(adapters);
}

/* We allocate space for the adapters in bunches. The last item is a
terminator, so here we start with room for 7 adapters, which should
be enough in most cases. If not, we allocate more later as needed. */
#define BUNCH 8

/* n must match the size of adapters at calling time */
static struct i2c_adap *more_adapters(struct i2c_adap *adapters, int n)
{
struct i2c_adap *new_adapters;

new_adapters = realloc(adapters, (n + BUNCH) * sizeof(struct i2c_adap));
if (!new_adapters) {
free_adapters(adapters);
return NULL;
}
memset(new_adapters + n, 0, BUNCH * sizeof(struct i2c_adap));

return new_adapters;
}

struct i2c_adap *gather_i2c_busses(void)
{
char s[120];
struct dirent *de, *dde;
DIR *dir, *ddir;
FILE *f;
char fstype[NAME_MAX], sysfs[NAME_MAX], n[NAME_MAX];
int foundsysfs = 0;
int count=0;
struct i2c_adap *adapters;

adapters = calloc(BUNCH, sizeof(struct i2c_adap));
if (!adapters)
return NULL;

/* look in /proc/bus/i2c */
if ((f = fopen("/proc/bus/i2c", "r"))) {
while (fgets(s, 120, f)) {
char *algo, *name, *type, *all;
int len_algo, len_name, len_type;
int i2cbus;

algo = strrchr(s, '\t');
*(algo++) = '\0';
len_algo = rtrim(algo);

name = strrchr(s, '\t');
*(name++) = '\0';
len_name = rtrim(name);

type = strrchr(s, '\t');
*(type++) = '\0';
len_type = rtrim(type);

sscanf(s, "i2c-%d", &i2cbus);

if ((count + 1) % BUNCH == 0) {
/* We need more space */

Advertising