forked from flashrom/flashrom
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathi2c_helper_linux.c
More file actions
62 lines (50 loc) · 1.37 KB
/
i2c_helper_linux.c
File metadata and controls
62 lines (50 loc) · 1.37 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
/*
* This file is part of the flashrom project.
*
* SPDX-License-Identifier: GPL-2.0-or-later
* SPDX-FileCopyrightText: 2020 The Chromium OS Authors
*/
#include "i2c_helper.h"
#include "programmer.h"
#include "platform/i2c.h"
#include <errno.h>
#include <string.h>
#include <stdlib.h>
static int get_bus_number(char *bus_str)
{
char *bus_suffix;
errno = 0;
int bus = (int)strtol(bus_str, &bus_suffix, 10);
if (errno != 0 || bus_str == bus_suffix) {
msg_perr("%s: Could not convert 'bus'.\n", __func__);
return -1;
}
if (strlen(bus_suffix) > 0) {
msg_perr("%s: Garbage following 'bus' value.\n", __func__);
return -1;
}
msg_pinfo("Using I2C bus %d.\n", bus);
return bus;
}
int i2c_open_from_programmer_params(const struct programmer_cfg *cfg, uint16_t addr, int force)
{
int fd = -1;
char *bus_str = extract_programmer_param_str(cfg, "bus");
char *device_path = extract_programmer_param_str(cfg, "devpath");
if (device_path != NULL && bus_str != NULL) {
msg_perr("%s: only one of bus and devpath may be specified\n", __func__);
goto out;
}
if (device_path == NULL && bus_str == NULL) {
msg_perr("%s: one of bus and devpath must be specified\n", __func__);
goto out;
}
if (device_path != NULL)
fd = i2c_open_path(device_path, addr, force);
else
fd = i2c_open(get_bus_number(bus_str), addr, force);
out:
free(bus_str);
free(device_path);
return fd;
}